Everything You Need to Know About Nullish Coeslacing Operator in Node.js
Do you know the difference between Nullish Coeslacingand Logical OR Operator
What is the Nullish Coalescing Operator?
It is a binary operator that returns its right-hand side operand if its left-hand side operand is nullish. (What exactly this means?)
Let’s take a look at the example below.
const text = null ?? 'I Love JS';
console.log(text); // Output: "I Love JS"
In short, it uses the default value that you declare on the right-hand side if the value on the left-hand side is nullish. The term nullish here refer to null
and undefined
.
How Nullish Coalescing Different from Logical OR Operator?
At this point, you may be wondering what is the difference between nullish coeslacing and Logical OR operator. I have the same question in my mind when I first encountered this. For example:
const text = null || 'I Love JS';
console.log(text); // Output: "I Love JS"
The above code generates the same output as the one using ??
.