🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
JS MASTER CLASS /// MASTER THE ENGINE /// BUILD LOGIC /// ASYNC PATTERNS /// JS MASTER CLASS /// MASTER THE ENGINE ///

Nullish Coalescing | JavaScript Tutorial - In-Depth Guide

Understand nullish coalescing in depth: how it differs from the logical OR operator, mixing rules with && and ||, and the nullish coalescing assignment operator.

Total XP: 0|💻 javascript XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

What does `0 ?? 10` evaluate to?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

The '??' operator fixes a long-standing footgun with '||' for default values: it only falls back on null or undefined, correctly preserving legitimate falsy values like 0, false, and empty strings.

1Nullish Coalescing | JavaScript Tutorial - In-Depth Guide Part 1

The '??' operator returns its right-hand side only when the left-hand side is null or undefined — unlike '||', which falls back on any falsy value.

+
const volume = 0;
volume || 10; // 10 (wrong!)
volume ?? 10; // 0 (correct)
localhost:3000
🕳️

?? vs ||

2Nullish Coalescing | JavaScript Tutorial - In-Depth Guide Part 2

'??' is the precise tool for 'use this value unless it was never actually set', as opposed to 'use this value unless it's falsy'.

+
function greet(name) {
  const who = name ?? 'friend';
  return `Hello, ${who}!`;
}
localhost:3000

Presence, Not Truthiness

3Nullish Coalescing | JavaScript Tutorial - In-Depth Guide Part 3

JavaScript forbids mixing '??' directly with '&&' or '||' without parentheses, to avoid ambiguous precedence.

+
a || b ?? c; // SyntaxError
(a || b) ?? c; // OK
localhost:3000

Mixing Requires Parens

4Nullish Coalescing | JavaScript Tutorial - In-Depth Guide Part 4

'??' chains left-to-right, returning the first operand that isn't null or undefined.

+
const theme = userTheme ?? orgTheme ?? 'light';
localhost:3000

Fallback Chains

5Nullish Coalescing | JavaScript Tutorial - In-Depth Guide Part 5

'??=' is the assignment form: it only assigns a new value to a variable if it is currently null or undefined.

+
let config = {};
config.retries ??= 3;
localhost:3000

The ??= Operator

6Step-by-Step Breakdown

The '??' operator returns its right-hand side only when the left-hand side is null or undefined — unlike '||', which falls back on any falsy value.

Checkpoint: What does 0 ?? 10 evaluate to?

  • 0, because 0 is not nullish
  • 10, because 0 is falsy

'??' is the precise tool for 'use this value unless it was never actually set', as opposed to 'use this value unless it's falsy'.

JavaScript forbids mixing '??' directly with '&&' or '||' without parentheses, to avoid ambiguous precedence.

Checkpoint: Can you write a || b ?? c without parentheses?

  • Yes, JS resolves it left-to-right
  • No, it throws a SyntaxError without explicit parentheses

'??' chains left-to-right, returning the first operand that isn't null or undefined.

'??=' is the assignment form: it only assigns a new value to a variable if it is currently null or undefined.

Next, we'll explore 'Logical Assignment Operators'.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Use `??` for ARIA Attribute Defaults Involving Booleans

An attribute like `aria-pressed={isPressed ?? false}` correctly preserves an explicit `false` value, whereas `isPressed || false` would behave identically here but the habit of using `||` for booleans elsewhere risks the same falsy-value bug on numeric ARIA values like `aria-valuenow`.

SEO Implications

  • 1

    Correct Defaults Prevent Silent Content Bugs

    A `||`-based default bug that replaces a legitimate 0 (like a "0 reviews" count) with a different fallback value can render incorrect content that misleads both users and search engines indexing structured data.

Best Practices

Default to `??` Over `||` for Value Defaults

Unless you specifically want to replace every falsy value (rare), `??` is almost always the operator you actually mean when writing 'default to X if not provided'.

Use `??=` for Lazy Initialization

A property that should only be set once, the first time it is undefined, is a perfect fit for `??=` instead of a verbose `if (x === undefined) x = ...` block.

Frequent Bugs

THE BUG

A numeric form field defaulted with `value || 0` silently replaces a user-entered `0` with the fallback, corrupting form state.

THE FIX

Switch to `value ?? 0`, which only substitutes the default when the value is actually null or undefined, leaving a real 0 untouched.

THE BUG

Writing `a || b ?? c` directly causes a SyntaxError at parse time, breaking the build.

THE FIX

Add explicit parentheses to state the intended precedence, e.g. `(a || b) ?? c`.

Real-World Examples

Defaulting a Pagination Page Size

An API client needed to default a `pageSize` query parameter to 20 only when the caller genuinely omitted it, while still allowing an explicit `0` to mean "no items" for a specific edge case.

function fetchPage({ pageSize } = {}) {
  const size = pageSize ?? 20;
  return fetch(`/items?limit=${size}`);
}

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Using `||` for numeric/boolean defaults

const isEnabled = settings.enabled ?? true; // preserves an explicit false

The Solution //

Replace with `??` whenever 0, false, or "" are valid, meaningful values that should not be overridden by a default.

Lesson Glossary

[01]Nullish Coalescing

The ?? operator, returning its right side only when the left side is null or undefined.

Code Preview
a ?? b

[02]Falsy Value

A value that coerces to false in a boolean context: 0, "", null, undefined, NaN, false.

Code Preview
0, "", NaN

[03]Nullish Value

Specifically null or undefined — a strict subset of falsy values.

Code Preview
null, undefined

[04]Logical OR (||)

An operator returning its right side when the left side is any falsy value.

Code Preview
a || b

[05]Nullish Assignment (??=)

Assigns a new value only if the variable is currently null or undefined.

Code Preview
x ??= 1;

Continue Learning