JS
…
← Back to blog

2026-09-14 · 4 min

?? vs || — why a price of 0 vanished

title || "Untitled" looks clever until title is "" or count is 0. ?? skips only null and undefined. count ?? 1 keeps 0. title ?? "Untitled" keeps "".

Use ?? at API boundaries: const items = body.items ?? []. Do not use || [] if an empty list and a missing list must be distinguished — then check Array.isArray and fail with a message.

Optional chaining pairs with it: user.address?.city ?? "unknown". That is daily test-helper code, not trivia.

Teams and tool vendors can sponsor this page.