What is the JavaScript double question mark operator

Learn what the JavaScript double question mark operator does, how it differs from ||, and how to use nullish coalescing for safe defaults and concise code.

JavaScripting
JavaScripting Team
·5 min read
Nullish coalescing operator

Nullish coalescing operator is a JavaScript operator ?? that returns the right-hand operand when the left-hand operand is null or undefined; otherwise it returns the left-hand operand.

The nullish coalescing operator is written as two question marks. It provides default values only when the left side is null or undefined, not for other falsy values like 0 or an empty string. This keeps your code concise and predictable when handling user input and optional data.

What the nullish coalescing operator does

In JavaScript, the double question mark operator ?? offers a concise way to supply defaults only when a value is null or undefined. If the left operand is non-nullish, its value is used as is. If it is null or undefined, the right-hand operand is evaluated and returned. This behavior is particularly helpful when you want to avoid treating valid falsy values like 0, false, or an empty string as missing. For the question javascript what is double question mark, this operator is the answer that many developers reach for to keep code clean and predictable.

JS
let a = null; let b = a ?? 5; // 5 let c = 0; let d = c ?? 7; // 0

As shown, nullish values trigger the default, while non-nullish values pass through unchanged.

How the ?? operator is evaluated and why it matters

The evaluation rule is straightforward: if the left-hand side is null or undefined, return the right-hand side; otherwise, return the left-hand side. This means that values like NaN, false, or the empty string do not trigger a fallback unless they are actually null or undefined. This subtle distinction is what makes ?? different from other defaulting operators and is essential when you are dealing with optional data in forms, API responses, or configuration objects.

JS
const config = { timeout: 0 }; const timeout = config.timeout ?? 3000; // 0, not 3000

The example demonstrates that 0 is a legitimate value and should not be replaced by a default.

Practical usage patterns for real world code

You can apply ?? in several common scenarios. When extracting values from objects where a field may be missing, use ?? to provide a safe default:

JS
const user = getUser(); const name = user?.name ?? "Guest";

If you are reading environment-like data or user input, ?? keeps defaults elegant without risking accidental overrides due to falsy values:

JS
function connect(opts) { const host = opts?.host ?? "localhost"; const port = opts?.port ?? 8080; return `Connecting to ${host}:${port}`; }

These patterns help maintain readable, robust code, especially when combined with optional chaining for deep property access.

Mixing ?? with other operators and functions

Nullish coalescing plays well with other operators, but you should be mindful of operator precedence. Always group expressions clearly to avoid surprises in complex defaults:

JS
const value = (a ?? b) ?? c;

You can also use ?? alongside logical operators in conditional flows, but avoid overcomplicating expressions. Keeping defaults near the data source improves readability and reduces bugs when refactoring.

Nested coalescing and precedence rules

You can chain ?? multiple times to provide a cascade of defaults. Evaluation stops at the first non-nullish value, preventing unnecessary computation:

JS
const x = null; const result = x ?? undefined ?? 42 ?? "fallback"; // 42

Remember that only nullish values (null or undefined) trigger the right-hand side, so reordering defaults can change outcomes. Plan your fallback priority carefully when building configuration objects or feature flags.

Common pitfalls and best practices when using ??

One common pitfall is mixing ?? with the logical OR operator in ways that produce unexpected results. Since || considers any falsy value, while ?? only nullish values, combining them without care can lead to inconsistent behavior. Prefer using ?? for defaults when you want to preserve 0, false, and "" as valid values. Another tip is to use optional chaining (?.) together with ?? to avoid runtime errors when accessing nested data structures.

Browser support and tooling considerations in 2026

The nullish coalescing operator is supported by all modern browsers and Node.js runtimes. If you still target older environments, enable a transpiler like Babel or TypeScript with the appropriate preset to convert ?? into equivalent code. This approach ensures you can adopt nullish coalescing in new codebases while maintaining compatibility with legacy systems.

Real world patterns and a few extended examples

Defaulting values directly in configuration objects is a common use case:

JS
function init(options) { const { mode = "default" } = options ?? {}; const retryCount = options?.retry ?? 3; return { mode, retryCount }; }

Combining optional chaining with ?? keeps error-free access and predictable fallbacks, even when data shapes vary across API responses. This technique reduces boilerplate and helps maintain a clean, readable codebase.

Questions & Answers

What does the nullish coalescing operator do in JavaScript?

The operator returns the right-hand operand when the left-hand operand is null or undefined; otherwise it returns the left-hand operand.

The ?? operator gives you the right-hand value only if the left side is null or undefined; otherwise it uses the left side.

Can ?? be used with non-nullish falsy values like 0 or empty string?

No. 0 and the empty string are not nullish, so they will be returned as the left-hand value unless you explicitly override them.

No. Zero and empty strings are not considered nullish, so they stay as the left value.

How is ?? different from ||?

?? checks specifically for null or undefined, while || treats any falsy value as missing. This distinction changes which values get defaults.

?? only kicks in for null or undefined; || uses any falsy value as a trigger.

Is ?? supported in all environments?

The operator is widely supported in modern browsers and Node.js. If you must support older runtimes, use a transpiler or polyfill strategy.

Yes in modern environments; for very old runtimes, use transpilation or Pollyfills.

Can I chain ?? with optional chaining?

Yes. You can combine optional chaining with nullish coalescing to safely access a value and provide a default when needed.

Yes, you can chain ?? with optional chaining for safe defaults.

What to Remember

  • Learn the core rule: ?? returns the right-hand side only when the left is null or undefined.
  • Compare with || to understand how falsy values like 0 or "" are treated differently.
  • Use ?? with optional chaining to safely access nested data and provide defaults.
  • Chain ?? carefully; evaluation stops at the first non-nullish value.
  • Ensure environment compatibility with your toolchain or transpilation when targeting older runtimes.

Related Articles