← Back to Chapters

JavaScript Nullish Coalescing

? JavaScript Nullish Coalescing

⚡ Quick Overview

The nullish coalescing operator ?? lets you provide a default value only when a variable is null or undefined. Unlike the logical OR operator (||), it does not treat other falsy values such as 0, false, or '' as “empty”.

This is especially useful when you want to keep valid falsy values and avoid accidentally overwriting them with a default.

? Key Concepts

  • ?? checks only for null and undefined (called “nullish” values).
  • Values like 0, false, and '' are kept as they are and do not trigger the default.
  • Useful for default values in API data, configs, and optional parameters.
  • Often combined with optional chaining (?.) to safely access nested properties.

? Syntax and Theory

Basic syntax of the nullish coalescing operator:

? View Code Example
let result = value ?? defaultValue;
// If value is null or undefined → result = defaultValue
// Otherwise                     → result = value

Think of ?? as “use the left side if it is defined, otherwise fall back to the right side”.

? Basic Usage

Use ?? to provide safe defaults without losing valid falsy values:

? View Code Example
let name;
let displayName = name ?? 'Guest';
console.log(displayName); // 'Guest'

let count = 0;
let total = count ?? 10;
console.log(total); // 0 (not 10, because 0 is not null or undefined)

? Difference from OR Operator

The logical OR operator (||) treats many values as falsy (0, '', false, null, undefined, etc.). The nullish coalescing operator (??) only treats null and undefined as needing a default.

? View Code Example
let value = '';
console.log(value || 'Default'); // 'Default' because '' is falsy
console.log(value ?? 'Default'); // '' because '' is not nullish

Use ?? when you want to preserve valid falsy values and only replace null or undefined.

? With Optional Chaining

Combine optional chaining (?.) and nullish coalescing for safer defaulting when dealing with nested objects or API responses.

? View Code Example
const user = {
profile: {
name: 'Alex'
}
};

const username = user.profile?.name ?? 'Anonymous';
console.log(username); // 'Alex'

const settings = {};
const theme = settings.preferences?.theme ?? 'light';
console.log(theme); // 'light' because preferences is undefined

? Live Output and Explanation

From the basic usage example:

  • name is undefined, so name ?? 'Guest''Guest'.
  • count is 0. Since 0 is not null or undefined, count ?? 100.

From the comparison with OR:

  • value || 'Default' returns 'Default' because '' is falsy.
  • value ?? 'Default' returns '' because the value is defined (not nullish).

This shows why ?? is safer when empty strings, zero, or false are meaningful values in your logic.

? Use Cases

  • Providing default values for optional configuration options.
  • Handling missing fields in API responses cleanly.
  • Setting fallback text for UI labels when data is null or undefined.
  • Safely reading nested properties with optional chaining and default values.

? Tips and Best Practices

  • Use ?? instead of || when 0, false, or '' are valid values.
  • Prefer value ?? default for user inputs or API fields where “empty” still has meaning.
  • Combine ?. and ?? to safely access deep properties and provide clean defaults.
  • Use parentheses when mixing ?? with logical operators (&&, ||) to keep the intent clear.

? Try It Yourself

  • Create a variable that is null and use ?? to print a default name.
  • Test ?? with values like 0, '', and false. Observe which ones are replaced.
  • Define an object with optional nested properties and use obj?.nested?.value ?? 'No value' to safely display a result.
  • Rewrite a piece of code that uses || for defaults and replace it with ??. Check how the behavior changes.