javascript null coalesce code example

Example 1: double question mark javascript

//Similar to || but only returns the right-hand operand if the left-hand is null or undefined
0 ?? "other" // 0
false ?? "other" // false
null ?? "other" // "other"
undefined ?? "other" // "other"

Example 2: nullish coalesing

Expression:
	Left ?? Right
if left is null or undefined , then Right will be the value 

const a = '' ;
const b = undefined;

const c = a ?? 'default' ; // will give c =''
const d = b ?? 'default' ; // wil give d = 'default'

Example 3: nullish-coalescing-operator

null || undefined ?? "foo"; // raises a SyntaxError
true || undefined ?? "foo"; // raises a SyntaxError

Tags:

Misc Example