New to Rust? Grab our free Rust for Beginners eBook Get it free →
JavaScript Double Question Mark Operator
Discover how the JavaScript double question mark operator works, see clear examples, and learn why the nullish coalescing operator matters. Explore more now.

Choosing a JavaScript fallback starts with deciding which values should trigger it. The double question mark operator, written ??, uses its right-hand value only when the left value is null or undefined.
I’ll show how to use ?? to provide a default for an optional property.
TL;DR
JavaScript’s double question mark, written ??, uses its right operand only when the left value is null or undefined.
- Choose ?? when zero, false, an empty string, or NaN can be valid data.
- Choose || when any falsy value should select the fallback.
- Put parentheses around ?? when it meets || or && in one expression.
What Is the JavaScript Double Question Mark Operator?
The nullish coalescing operator is written with two question marks.
| Left operand | Value returned by ?? |
|---|---|
| null or undefined | Right operand |
| Any other value | Left operand itself |
Null is an explicitly empty value, while undefined may indicate an unassigned variable or missing property and can also be assigned directly.
How to Choose and Use ?? in JavaScript Step by Step
Choose nullish coalescing when the input contract treats null and undefined as absent. Keep validation separate, and run the examples with Node.js v26.7.0.
Step 1: Compare nullish and falsy defaults
I ran node core-values-demo.js and saw zero with zero right-side calls in its final line.
const values = [
["zero", 0],
["empty string", ""],
["false", false],
["null", null],
["undefined", undefined],
["NaN", NaN],
];
function show(value) {
return typeof value === "string" ? JSON.stringify(value) : String(value);
}
for (const [label, value] of values) {
console.log(`${label}: ?? ${show(value ?? "default")} | || ${show(value || "default")}`);
}
let fallbackCalls = 0;
const keptZero = 0 ?? (fallbackCalls += 1, 10);
console.log(`zero: ${keptZero}; right-side calls: ${fallbackCalls}`);
The command printed the following seven output lines:
zero: ?? 0 | || "default" empty string: ?? "" | || "default" false: ?? false | || "default" null: ?? "default" | || "default" undefined: ?? "default" | || "default" NaN: ?? NaN | || "default" zero: 0; right-side calls: 0

The call counter shows short-circuiting: zero settles the expression, so JavaScript skips the fallback expression.
Step 2: Default an argument or optional property
An omitted argument is undefined, while explicit null remains a distinct input.
function greeting(name) {
return name ?? "Guest";
}
console.log(`omitted argument: ${greeting()}`);
console.log(`explicit null: ${greeting(null)}`);
console.log(`empty string: ${JSON.stringify(greeting(""))}`);
const user = { profile: null };
console.log(`optional property: ${user.profile?.name ?? "Anonymous"}`);
const primary = null;
const backup = "dark";
console.log(`first available setting: ${primary ?? backup ?? "system"}`);
function defaultParameter(name = "Guest") {
return name;
}
console.log(`default parameter with null: ${defaultParameter(null)}`);
I ran node optional-defaults-demo.js and got Guest for omitted and null inputs, while the empty string stayed empty.
omitted argument: Guest explicit null: Guest empty string: "" optional property: Anonymous first available setting: dark default parameter with null: null

Optional chaining turns the null profile into undefined, so nullish coalescing supplies Anonymous. A default parameter handles omitted or undefined arguments, but not null.
Use a parameter default when omission means “use Guest.” Choose nullish coalescing when explicit null or an object value should also select a fallback.
Parentheses can end an optional chain, leaving a later property access to evaluate on undefined.
Step 3: Assign a default with ??=
Use ??= to replace a nullish variable or property in place.
const preferences = { volume: 0 };
preferences.volume ??= 50;
preferences.theme ??= "dark";
let retryLimit;
retryLimit ??= 3;
console.log(`volume: ${preferences.volume}`);
console.log(`theme: ${preferences.theme}`);
console.log(`retry limit: ${retryLimit}`);
I ran node nullish-assignment-demo.js and confirmed that volume stayed zero. The missing theme and retry limit received defaults.
volume: 0 theme: dark retry limit: 3

The const binding stays fixed while its object properties can change. The right side of ??= runs only when the current value is nullish.
Step 4: Parenthesize expressions that mix operators
JavaScript rejects an unparenthesized mix of nullish coalescing and logical OR or AND. I ran node mixed-operators-error.js and got the parser error below. A try/catch cannot catch invalid syntax in the same file because parsing fails first.
const source = 'return 1 || null ?? "fallback";';
try {
new Function(source);
} catch (error) {
console.log(`${error.name}: ${error.message}`);
}
The captured output from node mixed-operators-error.js was the parser message below:
SyntaxError: Unexpected token '??'

I ran node grouped-operators-demo.js to compare two explicit groupings and their returned values.
const first = false;
const second = null;
const fallback = "backup";
console.log(`(first ?? second) || fallback: ${(first ?? second) || fallback}`);
console.log(`first ?? (second || fallback): ${first ?? (second || fallback)}`);
The output from node grouped-operators-demo.js shows both grouping results:
(first ?? second) || fallback: backup first ?? (second || fallback): false

The first grouping lets logical OR replace false with backup. The second returns false from nullish coalescing, so it skips the right branch.
Edge Cases: Grouping, Validation, and Missing Values
Fallback operators select values, while validation enforces field-specific rules.
| Input or expression | What ?? does | Next check |
|---|---|---|
| NaN | Keeps NaN as a present numeric value | numeric validation |
| Empty text | Keeps the empty string | Trim or validate first when blank text means missing |
| Missing property on a declared object | Receives undefined, so ?? can select its fallback | Use optional chaining when a parent object can also be nullish |
| Undeclared root identifier | Raises ReferenceError before evaluation | Declare the binding or test its existence separately |
| Direct ?? with || or && | Fails during parsing | Parenthesize the intended groups |
Number.isNaN detects numeric NaN without coercing a string input. Trim text first when spaces count as missing, then validate the normalized value before applying a fallback.
An undeclared root raises a ReferenceError before nullish coalescing can begin. A missing property on a declared object evaluates to undefined and can reach a fallback. If a getter throws, its exception occurs before the operator receives a value.
For partial updates, an omitted key can mean “keep” and null can mean “clear.” MDN’s Object.hasOwn() reference distinguishes a missing key from an own key set to undefined.
Precedence outside logical mixes
Operator precedence determines grouping outside the logical mix restriction.
| Operator | Relative position to ?? | What to know |
|---|---|---|
| Multiplication and addition | Higher | They group before ?? |
| Logical OR | Same level | Parentheses are required when mixed directly |
| Conditional operator | Lower | ?? groups before the condition result |
Parentheses clarify grouping in a conditional expression with nullish coalescing.
Conclusion
Match the fallback to possible input values, then group mixed expressions explicitly.
MDN documents the nullish coalescing operator and its grouping error. CodeForGeek’s guides cover JavaScript operators and null versus undefined.
FAQ
These FAQ answers address common questions about results and syntax.
Does ?? return a Boolean value?
No. It returns one of its operands, either the left value or the fallback on the right.
Can I chain more than two ?? operators?
Yes. JavaScript checks the values from left to right and returns the first one that is neither null nor undefined, or the final value if each earlier one is nullish.
Can ?? be used with a ternary expression?
Yes. The nullish coalescing operator has higher precedence than the conditional operator. Parentheses can make a complex expression clearer.
Is ?? supported in current browsers?
MDN marks it Baseline Widely available, with cross-browser support since July 2020. Check the target runtime if an application still supports an older engine.




