fra*_*cis 6 javascript logical-or ecmascript-2020 nullish-coalescing
ES2020 introduced the nullish coalescing operator (??) which returns the right operand if the left operand is null or undefined. This functionality is similar to the logical OR operator (||). For example, the below expressions return the same results.
const a = undefined
const b = "B"
const orOperator = a || b
const nullishOperator = a ?? b
console.log({ orOperator, nullishOperator })
Run Code Online (Sandbox Code Playgroud)
result:
{
orOperator:"B",
nullishOperator:"B"
}
Run Code Online (Sandbox Code Playgroud)
So how is the nullish operator different and what is its use case?