JS: Nullish Coalescing Operator

By Xah Lee. Date: .

New in JS2020.

x ?? y
Return x if x is not null nor undefined , else return y. This is usually used for assignement, for example, const color = obj.color ?? "red";
console.log((undefined ?? 4) === 4);
console.log((null ?? 4) === 4);

console.log((0 ?? 4) === 0);
console.log((1 ?? 4) === 1);
console.log(("" ?? 4) === "");
console.log(("x" ?? 4) === "x");

// all true
βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Regular Expression

Date

Set Object

Map Object

Number

Misc