← TechnologyThe essentials of

The ?? operator: keeping zero instead of replacing it with a default

Publisher: ResumeMaker • 1 source

Published: 22 Oct 2026 13:13 · Updated: 22 Oct 2026 13:13

AI source check: 21 Oct 2026 10:06. This is not editorial approval.

In a few sentences

In brief

?? uses a reserve value only at null or undefined. Zero, false and blank text can remain valid data.

Useful to know

The operator ?? returns the right value only if the left is null or undefined. In other cases, it retains the left, including when it is 0, false or ''. This is important when the "no value" differs from the "value is zero". [1]

For example, const stock = 0; const shown = stock ?? 10; gives 0. If stock is undefined, the result is 10. The number 10 here is a notional value by default, not a real availability.

Do not automatically use a reserve value for each empty field. In a user form, empty text can mean "not filled in", but for ?? it is not null or undefined. This rule must be determined separately.

In a catalog, a free product with a price of 0 should not become a product with a default price. Distinguishing absence from a valid zero value prevents such silent errors in performance.

Sources

For further detail and to check the information:

  1. [1] MDN — Nullish coalescing ↗