Ch. 1 · JavaScript

JavaScript Defaults: Nullish Coalescing and OR

JavaScript Defaults: Nullish Coalescing and OR. Learn the reasoning, a practical example, common mistakes and an interview exercise.

~2 min readbeginnerupdated Oct 3, 2026

Nullish coalescing falls back for null or undefined. Logical OR falls back for every falsy value. Preserve meaningful zero, false and empty-string values when applying defaults.

Step-by-step walkthrough

Step 1: Identify legitimate falsy values

List which values the domain permits before applying a fallback. Zero can mean no delay, false can disable a feature, and an empty string can be a deliberate label. Absence should not be inferred from truthiness when those values are valid.

Step 2: Separate absence from validation

Use nullish coalescing to handle null or undefined, then validate the selected value. A negative timeout is present but invalid; silently replacing it hides a configuration mistake. This separation also produces more useful error messages for candidates debugging external inputs.

Step 3: Trace each input category

Follow missing, null, zero and malformed input through both stages. The default selects an absent value; the validator accepts or rejects the resulting number. Optional chaining can help read nested values but does not establish that those values have valid types.

Worked scenario

timeout || 1000 replaces zero; timeout ?? 1000 preserves it. Validate whether zero is permitted separately.

function readDelay(options = {}) {
  const delay = options.delay ?? 1000;
  if (!Number.isFinite(delay) || delay < 0) {
    throw new Error('delay must be a nonnegative number');
  }
  return delay;
}
console.log(readDelay({delay: 0})); // 0
console.log(readDelay({})); // 1000
JavaScript

Walk through the example

The nullish expression preserves zero. Number.isFinite rejects numeric-looking strings rather than coercing them, and the range check rejects negatives. This example deliberately accepts null as absence. If null means an explicit reset or invalid input in your API, enforce that separate policy instead.

Common mistake

Optional chaining does not validate the type of a present property.

Verify the behavior

Cover undefined, null, zero, a positive number, a negative number, NaN and a string. Check that valid zero survives and that invalid supplied values throw instead of quietly becoming the default. If coercion is intended, test its accepted spellings explicitly.

Interview exercise

Default an optional page size.

Answer and reasoning

Use nullish coalescing for absence, then validate the number against domain constraints.

Follow-up discussion

Why not use OR everywhere? Its truthiness behavior collapses legitimate falsy values into absence. Is nullish coalescing always correct? No: it is correct when null and undefined share the documented missing-value meaning. Domain rules determine the operator, not stylistic preference.

Continue learning

Compare the scenario with the JavaScript interview questions and test your understanding with the JavaScript MCQs. For terminology and implementation details, consult the reference material.

More in JavaScript

read ✓JavaScript · mid

JavaScript Date and Timezone Pitfalls

Parse and format dates without timezone surprises: zero-based months, date-only versus date-time parsing, and why UTC storage avoids drift.

~3 min readread →
read ✓JavaScript · mid

JavaScript Number Precision and BigInt

Why 0.1 + 0.2 is not 0.3, where safe integer range ends, and when BigInt is the right tool for large identifiers and money.

~2 min readread →
esc