Nullish Coalescing Operator (??)
The “Nullish Coalescing” operator is a feature introduced in ECMAScript 2020 (ES11) JavaScript. It is represented by two question marks (??
) and is used to provide a default value for a variable if the variable is null
or undefined
, but not for other falsy values such as 0
, false
, or an empty string (""
).
The nullish coalescing (??
) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined
, and otherwise returns its left-hand side operand.
// Using the nullish coalescing operator
let variable1 = null;
let variable2 = "Hello";
let result1 = variable1 ?? "Default Value";
let result2 = variable2 ?? "Default Value";
console.log(result1); // Output: Default Value
console.log(result2); // Output: Hello