Nullish Coalescing

Nullish Coalescing

Updated: January 17, 2021
TypeScript Programming

// say you have some var hangin' around called `name`
let x: string = name ?? '(no name)'

?? is the nullish coalescing operator. It differs from || in that if you had (in the above example)

let x: string = name || '(no name)'

name equal to an empty string (which is falsey) x would then be assigned the value (no name). The nullish coalescing operator will only use the right-hand assignment for “nullish” values (null or undefined).