JavaScript Nullish Coalescing Operator (Live Playground)
In this tutorial, we will explore the nullish coalescing operator, and learn how to use it in our code.
Nullish Coalescing Operator (??
)
The nullish coalescing operator returns the right-hand side operand when the left-hand side operand is null
or undefined
. Otherwise, it returns the left-hand side operand.
Example:
const a = null;
const b = 'Hello, world!';
const result = a ?? b;
console.log(result); // Output: 'Hello, world!'
Live Playground, Try it Yourself
Conclusion
Understanding and using the nullish coalescing operator is essential for writing efficient and concise code in your JavaScript programs.