Nullish Coalescing
The nullish coalescing operator ?? provides fallback values in a safer way than || for many situations.
You learned earlier that || returns the first truthy value.
That is useful, but it has a problem:
const volume = 0 || 50;
console.log(volume); // 50This is wrong if 0 is a valid value.
The ?? operator solves this by only falling back when the value is null or undefined.
What Does Nullish Mean?
In JavaScript, nullish means:
nullundefined
Only these two values are considered nullish.
These values are falsy, but not nullish:
0""falseNaN
Basic Syntax
const result = leftValue ?? fallbackValue;If leftValue is not null or undefined, JavaScript returns leftValue.
If leftValue is null or undefined, JavaScript returns fallbackValue.
Example:
console.log("Asha" ?? "Guest"); // Asha
console.log(null ?? "Guest"); // Guest
console.log(undefined ?? "Guest"); // Guest?? Preserves Valid Falsy Values
Unlike ||, ?? does not reject all falsy values.
console.log(0 ?? 50); // 0
console.log("" ?? "Default"); // ""
console.log(false ?? true); // false
console.log(NaN ?? 100); // NaNThese values are not null or undefined, so they are preserved.
The || Problem
Using || for defaults can accidentally replace valid values.
function setVolume(userVolume) {
const volume = userVolume || 50;
console.log(`Volume set to: ${volume}`);
}
setVolume(0); // Volume set to: 50
setVolume(null); // Volume set to: 50
setVolume(80); // Volume set to: 80The problem is 0.
0 is falsy, so || uses the fallback.
But 0 can be a valid volume level.
The ?? Solution
Use ?? when only null or undefined should trigger the fallback.
function setVolume(userVolume) {
const volume = userVolume ?? 50;
console.log(`Volume set to: ${volume}`);
}
setVolume(0); // Volume set to: 0
setVolume(null); // Volume set to: 50
setVolume(undefined); // Volume set to: 50
setVolume(80); // Volume set to: 80Now 0 is preserved.
|| vs ??
| Value | value || "fallback" | value ?? "fallback" |
| --- | --- | --- |
| 0 | "fallback" | 0 |
| "" | "fallback" | "" |
| false | "fallback" | false |
| null | "fallback" | "fallback" |
| undefined | "fallback" | "fallback" |
| "hello" | "hello" | "hello" |
Use this rule:
- Use
||when any falsy value should fall back. - Use
??when only missing values should fall back.
Common Use Case: API Data
An API may return 0 as a valid count.
const apiResponse = {
count: 0
};
const postCount = apiResponse.count ?? 0;
console.log(postCount); // 0If the API returns null, you can use the fallback:
const apiResponse = {
count: null
};
const postCount = apiResponse.count ?? 0;
console.log(postCount); // 0Common Use Case: Configuration Defaults
Configuration values often use false, 0, or "" intentionally.
const config = {
timeout: 0,
isEnabled: false,
label: ""
};
const timeout = config.timeout ?? 3000;
const isEnabled = config.isEnabled ?? true;
const label = config.label ?? "Untitled";
console.log(timeout); // 0
console.log(isEnabled); // false
console.log(label); // ""Using || here would incorrectly replace all three values.
Chaining ??
You can chain ?? when there are multiple possible sources.
const configValue = null;
const envValue = undefined;
const defaultValue = "production";
const finalEnv = configValue ?? envValue ?? defaultValue;
console.log(finalEnv); // productionJavaScript returns the first value that is not null or undefined.
Another example:
const localSetting = undefined;
const serverSetting = "dark";
const defaultSetting = "light";
const theme = localSetting ?? serverSetting ?? defaultSetting;
console.log(theme); // darkMixing ?? with && or ||
JavaScript does not allow mixing ?? directly with && or || without parentheses.
This is a syntax error:
// const result = a || b ?? c;Use parentheses to make the order clear:
const result = (a || b) ?? c;Or:
const result = a || (b ?? c);The parentheses tell JavaScript exactly how to group the logic.
?? with Optional Chaining
?? is often used with optional chaining.
const user = {
profile: null
};
const displayName = user.profile?.name ?? "Guest";
console.log(displayName); // GuestOptional chaining safely returns undefined if the property path is missing.
Then ?? provides the fallback.
You will learn optional chaining in more detail next.
Common Mistakes
Using || When 0 Is Valid
const page = 0;
const currentPage = page || 1;
console.log(currentPage); // 1Use ??:
const currentPage = page ?? 1;
console.log(currentPage); // 0Thinking ?? Handles All Falsy Values
console.log("" ?? "Default"); // ""
console.log(false ?? true); // false?? only checks for null and undefined.
Forgetting Parentheses
// const value = a && b ?? c; // SyntaxErrorUse:
const value = (a && b) ?? c;Best Practices
- Use
??for defaults when0,"", orfalseshould remain valid. - Use
||only when all falsy values should trigger the fallback. - Use parentheses when combining
??with&&or||. - Pair
??with optional chaining for safe nested property access. - Choose the operator that communicates your intent clearly.
Summary
The nullish coalescing operator ?? is a modern way to provide fallback values.
Remember:
??returns the right side only when the left side isnullorundefined.- It preserves
0,"",false, andNaN. ||checks truthiness, while??checks nullishness.??is useful for API data and configuration defaults.- You can chain
??to check multiple fallback sources. - You must use parentheses when mixing
??with&&or||.
Next, you will learn optional chaining, which pairs naturally with nullish coalescing.