Skip to main content

How to Use the Nullish Coalescing Operator (??) in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

value ?? fallback gives you fallback when value is null or undefined, and value itself otherwise. Unlike ||, it keeps 0, '' and false, so a real zero or an empty note isn't replaced by your fallback.

Syntax​

Syntax
<left> ?? <right>
PartWhat it does
<left>The value you want to use.
<right>The fallback. It's used only when <left> is null or undefined, and it isn't even evaluated otherwise.

The whole expression has the value of <left>, unless <left> is null or undefined: then it has the value of <right>. MDN calls ?? the nullish coalescing operator, because null and undefined are the two "nullish" values.

The Example​

The example has four values that could each mean "nothing": null, undefined, an empty string and 0. It gives each one a fallback with ??, then does the same to 0 with ||, so you can see where the two operators differ.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which sets nickname to null, city to undefined, note to an empty string and seats to 0, then logs each one with a fallback after ??, and seats again with a fallback after ||.

main.js
const nickname = null;
const city = undefined;
const note = '';
const seats = 0;

console.log('nickname:', nickname ?? 'none');
console.log('city:', city ?? 'unknown');
console.log('note:', note ?? 'no note');
console.log('seats:', seats ?? 'no seats');
console.log('seats with ||:', seats || 'no seats');

console.log prints its values on one line, separated by a space, so each line starts with a label.

Step 2: Run It​

Terminal
node main.js
Output
nickname: none
city: unknown
note:
seats: 0
seats with ||: no seats

A zsh terminal where node main.js prints nickname: none, city: unknown, note: with nothing after it, seats: 0 and seats with ||: no seats.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • nickname ?? 'none' gave none, because nickname is null.
  • city ?? 'unknown' gave unknown, because city is undefined.
  • note ?? 'no note' kept the empty string. Nothing appears after note: because the value is '': an empty string isn't null or undefined, so ?? doesn't replace it.
  • seats ?? 'no seats' kept 0 for the same reason.
  • seats || 'no seats' replaced the 0. || uses its right side whenever the left side is falsy, which includes 0, '', false and NaN as well as null and undefined. When 0 is a real answer, such as zero seats left, || throws it away and ?? keeps it.

Common Mistakes​

MistakeWhat happensDo this instead
Mixing ?? with || or && without brackets, null || undefined ?? 'x'Node.js stops before running anything: SyntaxError: Unexpected token '??'Add brackets to say which part comes first: (null || undefined) ?? 'x' gave x
Using || for a default when 0, '' or false are real valuesThe real value is replaced by the fallback, as seats || 'no seats' showedUse ??
Expecting ?? to replace false or NaNfalse ?? 'x' is false and NaN ?? 'x' is NaN: only null and undefined countCheck for those values yourself if you need to
Using ?? on a variable that was never declarednotDeclared ?? 'x' throws ReferenceError: notDeclared is not definedDeclare the variable first. A missing property is fine: ({}).missing ?? 'fallback' gave fallback

Troubleshooting​

SymptomLikely causeFix
SyntaxError: Unexpected token '??'?? sits next to || or && without bracketsPut brackets around the || or && part
The fallback never shows, although the value looks emptyThe value is '', 0 or false, not null or undefinedThat's what ?? is for. Use || if every falsy value should get the fallback
A zero or an empty string turns into your fallbackThe code uses ||Change it to ??

FAQ​

Is the right side evaluated when it isn't needed?​

No. In a test, 'left' ?? pick() never called pick, while null ?? pick() called it once. So an expensive fallback, such as a function call, only runs when it's needed.

What does ??= do?​

a ??= b sets a to b only when a is null or undefined. A variable holding null became 'set', while one holding 0 stayed 0.

Can I chain several fallbacks?​

Yes: a ?? b ?? c gives the first of the three that isn't null or undefined.

Conclusion​

Use ?? when only "no value at all", null or undefined, should get a fallback, and || when every falsy value should. When you combine ?? with || or &&, add brackets, or the code won't run.

Additional Resources​