More posts

JS Tidbit: Nullish Coalescing

20 November 2020 (a year ago)🏷️ #javascript🏷️ #technology

🕰️ This is an old post

Please note that this article was posted quite a while ago and may now be out-of-date or inaccurate.

This short post introduces a useful JavaScript operator to help make your one-liners even more concise.

The specification was added formally in the 11th edition of ECMAScript. It is implemented as a logical operator to selectively return the result of one of two expressions (or operands) based on one of the expressions resolving to a "nullish" value. A nullish value in JavaScript is one that is null or undefined.

In particular, the operator - given by ?? - will return the right-hand side if the left-hand expression resolves to null or undefined, and otherwise returns the left-hand side.

const x = y ?? z;

In the example above, z will be returned if y is "nullish", and otherwise y will be returned.

Nullish coalescing is similar to (but stricter than) the more commonly-seen logical OR operator - given by || - which returns the result of the right-hand side expression if the left-hand side resolves to any falsy value, which includes nullish ones in addition to the boolean false, empty string (''), 0, NaN, etc.

More info is available on MDN and Wikipedia discusses null coalescing in other languages.

📲 Enjoyed this article? Subscribe to updates!

If you would like to read more posts like this, then you can subscribe via RSS.

Subscribe to an RSS feed