Nullish coalescing operator: ‘??’

This is a recent addition to the language. Old browsers may need polyfills.

  • if a is defined, then a,
  • if a isn’t defined, then b.
result = (a !== null && a !== undefined) ? a : b;
  • || returns the first truthy value.
  • ?? returns the first defined value.
let height = 0;alert(height || 100); // 100
alert(height ?? 100); // 0



  • The nullish coalescing operator ?? provides a short way to choose the first “defined” value from a list. It’s used to assign default values to variables:
// set height=100, if height is null or undefined
height = height ?? 100;
  • The operator ?? has a very low precedence, only a bit higher than ? and =, so consider adding parentheses when using it in an expression.
  • It’s forbidden to use it with || or && without explicit parentheses.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhay Jain

Abhay Jain

Developer with 3 yrs of industrial experience in developing scalable web applications.