⬅️ FEM TypeScript Fundamentals
Nullish coalescing ??
nullmeans there’s a value, and the value is nothingundefinedmeans the value isn’t available (yet)- similar to the JavaScript logical OR operator,
||. But unlike||, it doesn’t treat0and''as falsey. - aka “returning the first value that isn’t
nullorundefined”
1 ?? 'default' === 1
0 ?? 'default' === 0
'a' ?? 'default' === 'a'
'' ?? 'default' === ''
null ?? 'default' === 'default'
undefined ?? 'default' === 'default'function numberOrOne(n: number | undefined): number {
return n ?? 1;
}
[numberOrOne(3), numberOrOne(undefined), numberOrOne(0)];
// Result:
[3, 1, 0]