js null coalesce code example
Example 1: javascript null check
if (Var === null) {
//code goes here
}
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
let foo = { someFooProp: "hi" };
console.log(foo.someFooProp?.toUpperCase() ?? "not available"); // "HI"
console.log(foo.someBarProp?.toUpperCase() ?? "not available"); // "not available"