← Back to list

Nullish Coalescing in JavaScript

The Nullish Coalescing operator (??) is used to store a default value when a variable is either null or undefined.

Somanathkakade · 2025-09-22 16:35 · 0 claps · 0.4 min read
#nullish-coalescing
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3 🌐 · Web Development

Nullish Coalescing in JavaScript

The Nullish Coalescing operator (??) is used to store a default value when a variable is either null or undefined.

Null;

undefined;

let result=value1 ?? value2;

When the first value is null or undefined, then it returns the second value.

If the first value is not null or not undefined, then it returns the first value.

// First Example
let name=null;
let displayName=name ?? 'Guest';
console.log(displayName); // Guest;

// Second Example
let count1 = 0 || 5 ; // 5
let count2 = 0 ?? 5;  // 0

//Third Example
let user= null;
let userName= undefined;
let finalName= user ?? userName ?? "Anonymous";
console.log(finalName); // Anonymous

메타데이터
post_id
2bbc33052227
slug
nullish-coalescing-in-javascript-2bbc33052227
url
https://medium.com/@somanathkakade5/nullish-coalescing-in-javascript-2bbc33052227
canonical_url
https://medium.com/@somanathkakade5/nullish-coalescing-in-javascript-2bbc33052227
author_url
https://medium.com/@somanathkakade5
status
ok
fetched_at
2026-07-14 19:59:56