nullish coalescing javascript code example
Example 1: double question mark javascript
0 ?? "other"
false ?? "other"
null ?? "other"
undefined ?? "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' ;
const d = b ?? 'default' ;
Example 3: logical nullish operator javascript
function config(options) {
options.duration ??= 100;
options.speed ??= 25;
return options;
}
config({ duration: 125 });
config({});
Example 4: nullish-coalescing-operator
let foo = { someFooProp: "hi" };
console.log(foo.someFooProp?.toUpperCase() ?? "not available");
console.log(foo.someBarProp?.toUpperCase() ?? "not available");