Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Nullish Coalescing

The ?? operator returns its left value unless it is null or undefined, in which case it returns the right value. It is commonly used for defaults while preserving meaningful falsy values such as 0, false, and ''.

How it works

The nullish coalescing operator evaluates its left operand first. It returns that value when it is neither null nor undefined; otherwise it evaluates and returns the right operand. Chaining the operator selects the first non-nullish value, making it useful for defaults without treating values such as 0, false, or an empty string as missing.

Syntax

leftExpr ?? rightExpr

Parameters

NameTypeDescription
aany expressionExpression checked for null or undefined.
bany expressionFallback expression evaluated when the left expression is null or undefined.

Returns

The left operand if it is not null or undefined; otherwise, the right operand.

Key points

  • The operator uses the infix token ??.
  • A non-nullish left operand is returned unchanged.
  • A nullish left operand causes the right operand to be returned.
  • Use it to supply a fallback only when a value is null or undefined.
  • Chaining ?? selects the first available non-nullish value.
  • Its precedence matches the logical OR operator, so grouping may be needed.

Common mistakes

  • Because its precedence is low, use parentheses when combining ?? with larger expressions or other operators.

Example: Preserve defined values

const configured = 0;
const retries = configured ?? 3;
const timeout = null ?? 5000;

console.log(retries); // 0
console.log(timeout); // 5000

The operator preserves valid falsy values such as 0, but substitutes the fallback for null or undefined.

When to use it

Apply safe defaults

A setting may be absent but zero, false, or an empty string can be meaningful.

Choose the first available value

Several optional sources can provide the same value.

Avoid unnecessary fallback evaluation

A fallback expression is expensive or has side effects.

Interview questions

QWhat does the nullish coalescing operator do?

It returns the left operand unless that operand is null or undefined; otherwise it returns the right operand. Unlike ||, it does not replace other falsy values such as 0 or false.

Follow-up: When does the right operand run? It is evaluated only when the left operand is nullish, so the operator short-circuits.

QHow does ?? differ from ||?

Use ?? when a fallback should apply only to null or undefined. For example, a configured value of 0 should remain 0 rather than being replaced.

Follow-up: How can several possible values be searched? Chain the operator, such as first ?? second ?? fallback, to obtain the first non-nullish value.

QWhat restriction applies when mixing ?? with logical operators?

The expression a ?? b is valid, but combining ?? directly with && or || is not allowed. Parentheses make the intended grouping explicit, such as (a ?? b) || c.

Follow-up: Why is explicit grouping useful? It prevents precedence misunderstandings when a defaulting expression is embedded in a larger condition.

  • Logical OR operator ||
  • Logical AND operator &&
  • Conditional operator ?:

Source: Nullish coalescing operator '??'. Checked 2026-10-15.