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 })
result:
{
orOperator:"B",
nullishOperator:"B"
}
So how is the nullish operator different and what is its use case?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…