Null vs. Undefined
Checking for either
// Both null and undefined are only `==` to themselves and each other:
console.log(null == null); // true (of course)
console.log(undefined == undefined); // true (of course)
console.log(null == undefined); // true
// You don't have to worry about falsy values making through this check
console.log(0 == undefined); // false
console.log('' == undefined); // false
console.log(false == undefined); // falsefunction foo(arg: string | null | undefined) {
if (arg != null) {
// arg must be a string as `!=` rules out both null and undefined.
}
}Checking for root level undefined
Limit explicit use of undefined
undefinedNode style callbacks
Don't use undefined as a means of denoting validity
undefined as a means of denoting validityJSON and serialization
Final thoughts
Last updated