Sub topic 1
What Counts as Nullish
Optional chaining (?.) and nullish coalescing (??) both react to the same small set of values. Before you use either operator, you need to know exactly which values those are and how they differ from the broader group JavaScript calls "falsy."
Prerequisites
This chapter assumes you already know:
- JavaScript variables, objects and arrays
- Dot and bracket property access
- Calling functions and methods
- Truthy and falsy values
- The logical operators
||and&&
The Two Nullish Values
A value is nullish if it is null or undefined. No other value is nullish. JavaScript uses both values to mean "nothing is here," but they usually get there by different routes.
undefined is what the language gives you when a value was never provided:
let notSet;
const obj = {};
function noReturn() {}
function takesArg(x) { return x; }
console.log(notSet); // undefined (declared, never assigned)
console.log(obj.missing); // undefined (property does not exist)
console.log(noReturn()); // undefined (no return statement)
console.log(takesArg()); // undefined (argument not passed)
console.log([1, 2][5]); // undefined (index out of range)null almost never shows up by itself. A programmer or an API sets it on purpose to say "this has no value":
const user = { name: "Ada", manager: null };
console.log(user.manager); // null (explicitly empty)
console.log(JSON.parse('{"avatar": null}').avatar); // nullA common convention follows from this. undefined means "absent or not yet known," and null means "known to be empty." The nullish operators treat the two the same, so for them the distinction doesn't matter.
Nullish Is a Subset of Falsy
A falsy value is one that becomes false in a boolean context such as an if condition. Every nullish value is falsy, but most falsy values are not nullish.
| Value | Falsy? | Nullish? |
|---|---|---|
| `null` | Yes | Yes |
| `undefined` | Yes | Yes |
| `false` | Yes | No |
| `0` and `-0` | Yes | No |
| `0n` (BigInt zero) | Yes | No |
| `""` (empty string) | Yes | No |
| `NaN` | Yes | No |
| `"0"`, `[]`, `{}` | No | No |
This gap explains why the nullish operators exist. Values like 0, "" and false are often real, meaningful data: a volume of zero, an empty search box, a disabled setting. A check that treats every falsy value as missing will discard them.
Testing for Nullish Values
You can test for nullish values in three ways:
function isNullishStrict(v) {
return v === null || v === undefined;
}
function isNullishLoose(v) {
return v == null; // loose equality: true only for null and undefined
}
for (const v of [null, undefined, 0, "", false, NaN]) {
console.log(String(v), isNullishStrict(v), isNullishLoose(v), !v);
}
// null true true true
// undefined true true true
// 0 false false true
// false false true
// false false false true
// NaN false false trueThe !v column shows the mistake: it flags every falsy value. Loose equality v == null is the one place where == is commonly recommended, because it matches exactly null and undefined and nothing else. The ?? and ?. operators perform this same check for you.
Common Errors
- Treating
0,""orfalseas "missing" because they are falsy. - Writing
v === nulland forgetting that the value might beundefined, or the reverse. - Assuming
NaNis nullish. It is falsy but is a number value. - Assuming
typeof v === "object"excludesnull.
Exercise
For each value, say whether it is nullish: 0, undefined, "", null, NaN, false, [].
Show answer
Only undefined and null are nullish. 0, "", NaN and false are falsy but not nullish, and [] is truthy.
Exercise
What does const o = { a: undefined }; console.log(o.a == null, "a" in o, o.b == null, "b" in o); print, and why?
Show answer
It prints true true true false. Both o.a and o.b read as undefined, so both pass the == null check. The in operator shows that a exists as a property while b does not.