Widely availablePreferred for defaults when 0, false, or an empty string are valid states rather than missing data.

Overview

The nullish coalescing operator returns a fallback only when the left side is null or undefined. Unlike ||, it preserves 0, false, and empty strings.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
80
80
72
13.1
80
13.4
Other

The nullish coalescing assignment (??=) operator, also known as the logical nullish assignment operator, only evaluates the right operand and assigns to the left if the left operand is nullish (null or undefined).

85
85
79
14
85
14
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)

Syntax

JAVASCRIPT
const value = null ?? 'default';  // 'default'
const zero = 0 ?? 'default';      // 0
const empty = '' ?? 'default';    // ''

// Difference from ||
0 || 'default';  // 'default' (0 is falsy)
0 ?? 'default';  // 0 (0 is not null or undefined)

Live demo

Defaults without losing zero

Compare || and ?? to see why numeric zero often needs the nullish version.

JavaScript
Output
Press the Run button

Configuration fallback

Apply defaults only to missing options while keeping explicit empty or false values.

JavaScript
Output
Press the Run button

Use cases

  • Applying defaults safely

    Keep explicit values such as 0 or an empty label instead of replacing them accidentally.

  • Reading optional settings

    Use ?? when configuration objects may omit a field but should preserve intentionally empty values.

Cautions

  • ?? is about missing values only; if falsy values should also fall back, || may still be the right tool.
  • Mixing ?? with || or && requires parentheses in JavaScript syntax.

Accessibility

  • When you preserve empty strings intentionally, make sure the interface still exposes an accessible label or help text elsewhere.

Powered by web-features