How to Use the Nullish Coalescing Operator (??) in JavaScript
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
<left> ?? <right>
| Part | What 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:

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
node main.js
nickname: none
city: unknown
note:
seats: 0
seats with ||: no seats

The command is the same in zsh, bash and PowerShell.
What Happened
nickname ?? 'none'gavenone, becausenicknameisnull.city ?? 'unknown'gaveunknown, becausecityisundefined.note ?? 'no note'kept the empty string. Nothing appears afternote:because the value is'': an empty string isn'tnullorundefined, so??doesn't replace it.seats ?? 'no seats'kept0for the same reason.seats || 'no seats'replaced the0.||uses its right side whenever the left side is falsy, which includes0,'',falseandNaNas well asnullandundefined. When0is a real answer, such as zero seats left,||throws it away and??keeps it.
Common Mistakes
| Mistake | What happens | Do 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 values | The real value is replaced by the fallback, as seats || 'no seats' showed | Use ?? |
Expecting ?? to replace false or NaN | false ?? 'x' is false and NaN ?? 'x' is NaN: only null and undefined count | Check for those values yourself if you need to |
Using ?? on a variable that was never declared | notDeclared ?? 'x' throws ReferenceError: notDeclared is not defined | Declare the variable first. A missing property is fine: ({}).missing ?? 'fallback' gave fallback |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
SyntaxError: Unexpected token '??' | ?? sits next to || or && without brackets | Put brackets around the || or && part |
| The fallback never shows, although the value looks empty | The value is '', 0 or false, not null or undefined | That's what ?? is for. Use || if every falsy value should get the fallback |
| A zero or an empty string turns into your fallback | The 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
- Nullish coalescing operator (??), on MDN.
- Nullish coalescing assignment (??=), on MDN.
- Logical OR (||), on MDN.
