Nullish Coalescing
The ?? operator returns its right-hand side only when the left-hand side is null or undefined. Every other value, including 0, "" and false, is passed through.
Detailed Explanation
For years the standard way to supply a default was const port = config.port || 3000. The || operator returns the right side whenever the left side is falsy, and JavaScript has eight falsy values: false, 0, -0, 0n, "", null, undefined, and NaN.
That is too aggressive. If a user deliberately sets volume: 0, || throws the zero away and hands back the default. If a user clears a text field to "", || restores the old placeholder. These bugs are hard to spot in review because the code looks obviously correct.
??, added in ES2020, narrows the test. It asks only "is this null or undefined?" — a condition the spec calls nullish. A deliberate 0 or "" survives.
The rule of thumb: use || when you mean "is this empty or meaningless", and use ?? when you mean "was this value provided at all". Configuration, counts, prices, coordinates, and text fields almost always want ??.
Two other things behave identically to ??, and knowing this makes the family coherent. Default parameter values (function f(x = 1)) and destructuring defaults (const { x = 1 } = obj) also fire only on undefined. They are stricter still: they ignore null. So f(null) gives x === null, while null ?? 1 gives 1.
?? short-circuits. If the left side is not nullish, the right side is never evaluated. That makes value ?? expensiveDefault() safe to write.
There is a hard syntax rule. You cannot mix ?? with || or && at the same level without parentheses. a || b ?? c is a SyntaxError, not a precedence puzzle. The committee made it an error deliberately, because no default precedence would match everyone's intuition. Write (a || b) ?? c and state what you mean. ?? does combine freely with ?., and the pair is idiomatic: user.settings?.theme ?? "light".
The logical assignment operator ??= (ES2021) applies the same test in place. options.retries ??= 3 assigns only when retries is nullish. It also short-circuits, so no write happens when the value is present — which matters if the property has a setter or the object is being observed by a reactive framework.
There is no runtime cost worth measuring; ?? compiles to a nullish check. The real cost is that transpiling for very old targets expands it into a temporary-variable dance, adding a little bundle size. Every current browser and Node 14+ supports it natively.
Code Examples
The bug ?? was introduced to fix.
// The bug ?? was introduced to fix.
const settings = { volume: 0, label: "", retries: null };
console.log(settings.volume || 50); // => 50 wrong, the user chose 0
console.log(settings.volume ?? 50); // => 0 correct
console.log(settings.label || "Untitled"); // => Untitled discards the empty string
console.log(settings.label ?? "Untitled"); // => "" keeps the user's choice
console.log(settings.retries ?? 3); // => 3 null is nullish
console.log(settings.missing ?? 3); // => 3 undefined is nullish?? pairs with ?. for a safe read plus a fallback.
// ?? pairs with ?. for a safe read plus a fallback.
const response = { data: { user: { theme: null } } };
const theme = response.data?.user?.theme ?? "light";
console.log(theme); // => light
// Defaults in parameters and destructuring fire on undefined ONLY, not null.
function connect(host, port = 5432) {
return `${host}:${port}`;
}
console.log(connect("db")); // => db:5432
console.log(connect("db", null)); // => db:null default did not apply
console.log(connect("db", null ?? 5432)); // => db:5432
// ??= assigns in place, and skips the write entirely when already set.
const options = { timeout: 0 };
options.timeout ??= 1000; // no write, 0 is not nullish
options.retries ??= 3; // writes, retries was undefined
console.log(options); // => { timeout: 0, retries: 3 }
// Mixing without parentheses is a SyntaxError:
// const x = a || b ?? c; <- refuses to parseCommon Gotchas
`||` is not a drop-in equivalent
`||` replaces `0`, `""`, `NaN` and `false` as well. Swapping `??` for `||` in existing code can change behaviour silently.
Mixing with `||` or `&&` is a syntax error
`a ?? b || c` refuses to parse. Add parentheses to make the grouping explicit.
Parameter defaults are stricter
`function f(x = 1)` and `const { x = 1 } = obj` trigger on `undefined` only, so an explicit `null` gets through. `??` catches both.
`??=` skips the assignment
when the value is already present nothing is written, which avoids firing setters or reactivity in frameworks.