JavaScript ??

Beginner
5 min read
Updated: Nov 2026
3 examples

Definition and Usage

The nullish coalescing operator ?? returns the right side only when the left side is null or undefined. Unlike ||, it keeps 0 and "".

JavaScript Syntax

JavaScript
console.log(0 ?? 10);
console.log("" ?? "x");
console.log(null ?? "x");
Try it Editor

How It Works

  1. 0 stays. Not nullish.
  2. "" stays. Empty string is kept.
  3. null falls back.

Examples

Example 1 — Compare with ||

|| treats all falsy values as missing.

JavaScript
console.log(0 || 10);
console.log(0 ?? 10);
Try it Editor

How It Works

  1. || replaces 0.
  2. ?? keeps 0.

Example 2 — With optional chaining

Common API pattern.

JavaScript
const cfg = {};
console.log(cfg.port ?? 3000);
Try it Editor

How It Works

  1. Missing port → default 3000.

Example 3 — undefined also triggers

Both null and undefined.

JavaScript
console.log(undefined ?? "yes");
Try it Editor

How It Works

  1. undefined is nullish.

Frequently Asked Questions

When 0, false, or "" are valid values you must keep.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful