The Null coalescing operator (??) is used to provide a default value for a variable if its current value is null or undefined. It is a shorthand way of writing a common pattern used for default values.
Syntax as follows
let result = someVariable ?? defaultValue;
Explanation
The ?? operator checks if someVariable is null or undefined. If it is, defaultValue is assigned to result; otherwise, the current value of someVariable is assigned to result.
An Example
let x = null;
let y = "Hello, world!";
let resultX = x ?? "Default Value X";
let resultY = y ?? "Default Value Y";
console.log(resultX); // Output: Default Value X
console.log(resultY); // Output: Hello, world!
Explanation for the above example
resultX is assigned the default value because x is null, while resultY gets the value of y because it is not null or undefined.