A common practice in programming is to give default values to variables if they are either undefined or null.
// a is undefined var a; var test = a ?? 100; // "100" console.log(test);
var a = 34; var test = a ?? 100; // "34" console.log(test);
// c does not exist var test = c ?? 100; // "100" console.log(test);
If the left side expression is either null or undefined, then ?? operator returns the result of the right side expression. If not, then the left side expression is returned.
Nullish coalescing operator works in Firefox, Chrome & Edge. Safari is yet to implement it.