double question mark in js
The double question mark in js is ??, the nullish coalescing operator. It returns the right side when the left side is null or undefined. Any other value, including 0, "", and false, is kept.
const foo = null ?? "default string";
const baz = 0 ?? 42;
foo is "default string" because the left side is null. baz is 0 because 0 is not null or undefined.
Nullish vs falsy
|| returns the right side when the left side is falsy. Falsy values include null, undefined, 0, -0, 0n, "", false, and NaN. ?? steps in only for null and undefined, so a real 0 or an empty string stays in place.
const count = 0;
const text = "";
const qtyOr = count || 42;
const qtyNullish = count ?? 42;
const messageOr = text || "hi";
const messageNullish = text ?? "hi";
count || 42 is 42, because 0 is falsy. count ?? 42 is 0. text || "hi" is "hi". text ?? "hi" is "".
The same split shows up for the other falsy values. In the table, OR means || and Nullish means ??. The right side in every row is the string "default".
| Left side | OR "default" | Nullish "default" |
|---|---|---|
null | "default" | "default" |
undefined | "default" | "default" |
0 | "default" | 0 |
"" | "default" | "" |
false | "default" | false |
NaN | "default" | NaN |
0n || "default" is "default", and 0n ?? "default" is 0n. -0 follows 0: || replaces it, and ?? keeps it.
Parentheses with && and ||
?? has lower precedence than || and higher precedence than the conditional operator ? :. You cannot place ?? directly next to && or ||. Those lines are a SyntaxError.
null || undefined ?? "foo";
true && undefined ?? "foo";
Parentheses pick an order the parser accepts:
const fromOr = (null || undefined) ?? "foo";
const fromAnd = true && (undefined ?? "foo");
null || undefined is undefined, so fromOr is "foo". undefined ?? "foo" is "foo", and true && "foo" is "foo", so fromAnd is "foo".
The ternary needs no extra parentheses, because ?? binds tighter than ? :.
const label = 0 ?? "yes" ? "kept" : "replaced";
0 ?? "yes" is 0. The condition 0 is falsy, so label is "replaced".
Short-circuiting
The right side of ?? runs only when the left side is null or undefined.
function getDefault() {
console.log("getDefault ran");
return "default";
}
const kept = "hello" ?? getDefault();
const filled = null ?? getDefault();
kept is "hello", and getDefault does not run. filled is "default", and the function logs getDefault ran before it returns.
Optional chaining
?. stops when the value on its left is null or undefined, and the whole chain evaluates to undefined instead of throwing. ?? can then fill in a default. A value that is present stays, including "" and 0.
const foo = { someFooProp: "hi" };
const upper = foo.someFooProp?.toUpperCase() ?? "not available";
const missing = foo.someBarProp?.toUpperCase() ?? "not available";
const blank = { name: "" }.name ?? "Guest";
someFooProp is "hi", so toUpperCase() runs and upper is "HI". someBarProp is undefined, so ?. does not call toUpperCase, and missing is "not available". blank is "", because an empty string is not null or undefined.
Nullish coalescing assignment
??= assigns the right side only when the left side is null or undefined. It does not replace 0 or "". When the left side already has a value, the right side is not evaluated.
function config(options) {
options.duration ??= 100;
options.speed ??= 25;
return options;
}
config({ duration: 125 }) returns { duration: 125, speed: 25 }. duration is already 125, so it stays. speed was missing, which means undefined, so it becomes 25. config({}) returns { duration: 100, speed: 25 }. config({ duration: null }) sets duration to 100, because null is nullish, and sets speed to 25. config({ speed: 0 }) leaves speed at 0 and sets duration to 100.
A const that already holds a value is left alone, so this does not throw:
const x = 1;
x ??= 2;
x is still 1. No assignment happens. A const that is null does throw, because the assignment really runs:
const y = null;
y ??= 1;
That line throws a TypeError.
The right side is skipped when there is nothing to assign:
let n = 0;
let ran = false;
n ??= (ran = true);
n stays 0, and ran stays false.
Browser support
?? has been available across browsers since July 2020. Chrome 80, Edge 80, Firefox 72, Safari 13.1, and Node.js 14.0.0 include it.
??= has been available across browsers since September 2020. Chrome 85, Edge 85, Firefox 79, Safari 14, and Node.js 15.0.0 include it.