Nullish Coalescing
# Nullish Coalescing
This is a new operator `??` which is meant to augment
the common usage of `||` in the same way `===` augments `==`
as a more strict form of equality.
To understand it, let's look a how || works:
// These two examples work similar in most languages. As a tool || is
pretty good at defaulting things, but JavaScript's falsy checks
can surprise you for some common values:
Potentially unintended. '' is falsy, result: 'Hello, world!'
const response = {
nullValue: null,
headerText: "",
animationDuration: 0,
height: 400,
showSplashScreen: false,
} as const;
const undefinedValue = response.undefinedValue || "some other default";
// This would be: 'some other default'
const nullValue = response.nullValue || "some other default";
// When switching to use ?? instead, then === equality is used
to compare the two sides:
const headerText = response.headerText || "Hello, world!";
// Potentially unintended. 0 is falsy, result: 300
const animationDuration = response.animationDuration || 300;
// Potentially unintended. false is falsy, result: true
const showSplashScreen = response.showSplashScreen || true;
const emptyHeaderText = response.headerText ?? "Hello, world!";
const zeroAnimationDuration = response.animationDuration ?? 300;
const skipSplashScreen = response.showSplashScreen ?? true;