QUESTION 07 / 20 · COMPARISONS · INTERMEDIATE

When zero is a value

What will this code print? Read each line, then make your prediction.

Where this matters

A count of zero can be meaningful. Choose a defaulting rule that preserves it when only missing values should be replaced.

JavaScript · read-only example
console.log(0 ?? 10);
console.log(0 || 10);
Your prediction

Read the full explanation

Nullish coalescing replaces only null and undefined. OR replaces every falsy left operand, including zero.

Trace the code

  1. The left value is zero in both expressions.
  2. The ?? operator checks only for null or undefined; zero passes through unchanged.
  3. The || operator tests truthiness, so it returns the right operand for zero. Neither operator always returns a boolean.

Why the other predictions differ

  • 10 10 — Zero is not nullish, so ?? preserves it.
  • 0 0 — OR chooses ten because zero is falsy.
Expected console output
0
10

Reading this explanation does not record mastery. A correct submitted attempt can do so, including after review.

Change one thing

Replace zero with null. Both defaulting rules now select the fallback.

Predict the result, then run this self-contained snippet in your browser’s developer console. It uses only sample values and makes no network requests.

console.log(null ?? 10); console.log(null || 10);
Check the variation’s output
10
10

Explain which binding, value, or operation changed before moving to the next challenge.