Nullish coalescing operator

 Nullish coalescing operator


The nullish coalescing operator (??) is a new operator in JavaScript that provides a more reliable way to check for null or undefined values. It returns the first operand if it's not null or undefined, otherwise it returns the second operand. Here's an example:


const foo = null ?? 'default value';

console.log(foo); // Output: 'default value'


const bar = 0 ?? 'default value';

console.log(bar); // Output: 0 (because 0 is not null or undefined)

In the code above, the nullish coalescing operator (??) is used to provide default values for variables (foo and bar) that may be null or undefined.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...