Nullish Coalescing Operator (??) In Javascript

The nullish coalescing operator returns the left hand side if it is not null or undefined, otherwise it returns the right hand side. This makes it different from the logical OR (||) operator which returns the right hand side if the left side is falsey. Let’s see some examples to see exactly the behavior.

const foo = undefined ?? 'my string'
console.log(foo)
// "my string"

const num = 0 ?? 23
console.log(num)
// 0

It is important to note that the operator only returns the right hand side if the left hand side is null or undefined – but not false:

const huh = false ?? 'naw'
console.log(huh)
// false

Contrarily the logical OR returns the right hand side if the left side is just “falsey”.

const num = 0 || 23
console.log(num)
// 23

const huh = false || 'naw'
console.log(huh)
// "naw"

Read more about it here