nullish coalescing operator | javascript

PHOTO EMBED

Thu May 02 2024 06:35:07 GMT+0000 (Coordinated Universal Time)

Saved by @codejck #javascript

// nullish coalescing operator
let substitute = 51;

let val1 = null ?? 10;
console.log(val1);  //10

let val2 = undefined ?? substitute;
console.log(val2);  //51

let val3 = 5 ?? 8;
console.log(val3);  //5

let original = null;
let test = undefined;
let val4 = original ?? test ?? substitute
console.log(val4);  //51
content_copyCOPY

treat the null or undefined value with a particular value to eliminate garbage value or avoid errors.