So let’s take a stroll down project memory lane to think of a time you needed to make a comparison. Let’s say you have a particular variable that is in one of three states: falsy, defined, and null. Pretty standard.
For the code you’re writing, there is a particular scenario that you want to trigger when said variable is in the null
state. In the past, we would have to use the ===
operator to see if it would equal null. Nothing too complex, and is reliable. While this is great and functional, there is now a much simpler way to do this. Nullish Coalescing.
console.log(null ?? "null value test");
// "null value test"
console.log(undefined ?? "undefined test");
// undefined
console.log("test123" ?? "test156");
// "test123"
As you can see from the code above, the operator used is simpler and still achieves the same result as the prior used technique.