The logical OR operator (||) and nullish coalescing operator (??) are both used to provide a fallback value when the value on the left side is not suitable. However, they behave differently when the left side contains values such as 0, false, or an empty string (""). The || operator considers all falsy values as a reason to use the fallback, while the ?? operator uses the fallback only when the left side is null or undefined. Understanding this difference is important when working with default values, user input, configuration settings, and optional data in JavaScript.
Table of Contents
What is || in JavaScript?
The logical OR operator (||) is used to evaluate two expressions and return a value based on their truthiness. It can also be used to provide a fallback value when the first value is falsy.
For example:
// JavaScript program to demonstrate the logical OR operator let username = ""; let result = username || "Guest"; console.log(result);
Guest
Here, username is an empty string, which is a falsy value. Therefore, || returns "Guest".
What is ?? in JavaScript?
The nullish coalescing operator (??) is used to provide a fallback value only when the value on the left side is null or undefined.
For example:
// JavaScript program to demonstrate the nullish coalescing operator let username = ""; let result = username ?? "Guest"; console.log(result);
The empty string is not null or undefined, so ?? keeps the empty string instead of returning "Guest".
How Does || Work?
The || operator checks whether the value on the left side is truthy.
If the left-side value is truthy, it returns that value. If it is falsy, it returns the value on the right side.
The commonly encountered falsy values are:
- false
- 0
- -0
- 0n
- ""
- null
- undefined
- NaN
For example:
// JavaScript program to demonstrate fallback values using the OR operator let value1 = 0; let value2 = false; let value3 = ""; let value4 = null; console.log(value1 || 100); console.log(value2 || true); console.log(value3 || "Guest"); console.log(value4 || "Default");
100
true
Guest
Default
The || operator uses the right-side value whenever the left-side value is falsy.
How Does ?? Work?
The ?? operator checks specifically for null and undefined.
If the left-side value is null or undefined, the right-side fallback value is returned.
Otherwise, the left-side value is returned.
// JavaScript program to demonstrate fallback values using the nullish coalescing operator let value1 = 0; let value2 = false; let value3 = ""; let value4 = null; console.log(value1 ?? 100); console.log(value2 ?? true); console.log(value3 ?? "Guest"); console.log(value4 ?? "Default");
0
false
Default
Unlike ||, the ?? operator does not replace 0, false, or an empty string.
|| vs ?? with 0
This is one of the most important differences between the two operators.
With ||, 0 is considered falsy.
// JavaScript program to compare OR and nullish coalescing with zero let score = 0; console.log(score || 50); console.log(score ?? 50);
50
0
The || operator returns 50 because 0 is falsy.
The ?? operator returns 0 because 0 is not nullish.
This difference is especially important when 0 is a valid value.
|| vs ?? with false
The Boolean value false is also falsy.
// JavaScript program to compare OR and nullish coalescing with false let isLoggedIn = false; console.log(isLoggedIn || true); console.log(isLoggedIn ?? true);
true
false
The || operator replaces false because it is falsy.
The ?? operator keeps false because it is neither null nor undefined.
|| vs ?? with Empty String
An empty string is falsy, but it is not nullish.
// JavaScript program to compare OR and nullish coalescing with an empty string let name = ""; console.log(name || "Guest"); console.log(name ?? "Guest");
Guest
The || operator returns "Guest" because the empty string is falsy.
The ?? operator keeps the empty string because it is a valid non-nullish value.
|| vs ?? with null
Both operators return the fallback value when the left side is null.
// JavaScript program to compare OR and nullish coalescing with null let username = null; console.log(username || "Guest"); console.log(username ?? "Guest");
Guest
Guest
In this case, both operators produce the same result.
|| vs ?? with undefined
Both operators also return the fallback value when the left side is undefined.
// JavaScript program to compare OR and nullish coalescing with undefined let username; console.log(username || "Guest"); console.log(username ?? "Guest");
Guest
Guest
The variable username has the value undefined, so both operators use the fallback value.
Difference Between || and ??
The main difference is how the two operators treat falsy values.
| Value | value || "Default" | value ?? "Default" |
|---|---|---|
| 0 | "Default" | 0 |
| false | "Default" | false |
| "" | "Default" | "" |
| null | "Default" | "Default" |
| undefined | "Default" | "Default" |
| NaN | "Default" | NaN |
The || operator checks truthiness, while the ?? operator checks specifically for nullish values.
Using || for Default Values
The || operator is useful when all falsy values should be treated as missing or invalid.
For example:
// JavaScript program to demonstrate default values using OR let username = ""; let displayName = username || "Guest"; console.log(displayName);
Guest
Here, an empty username is treated as unavailable, so "Guest" is used.
Using ?? for Default Values
The ?? operator is useful when 0, false, and "" are valid values and should not be replaced.
// JavaScript program to demonstrate default values using nullish coalescing let quantity = 0; let selectedQuantity = quantity ?? 1; console.log(selectedQuantity);
0
Here, 0 is a valid quantity, so ?? keeps it instead of replacing it with 1.
Combining ?? with Optional Chaining
The nullish coalescing operator can be combined with optional chaining (?.) to safely access a value and provide a fallback.
// JavaScript program to demonstrate optional chaining with nullish coalescing
let student = {
name: "Tanavi"
};
let city = student.address?.city ?? "Unknown";
console.log(city);
Unknown
The optional chaining operator prevents an error when address does not exist. The ?? operator then provides "Unknown" because the result is undefined.
|| and ?? with Functions
Both operators can be used with function results.
For example:
// JavaScript program to demonstrate OR with a function result
function getName() {
return "";
}
let name = getName() || "Guest";
console.log(name);
Guest
Using ?? produces a different result:
// JavaScript program to demonstrate nullish coalescing with a function result
function getName() {
return "";
}
let name = getName() ?? "Guest";
console.log(name);
The empty string is preserved because it is not null or undefined.
Important Syntax Rule
JavaScript does not allow ?? to be directly mixed with || or && without parentheses.
For example, this causes a syntax error:
a ?? b || c
Use parentheses to clearly define the intended order.
// JavaScript program to demonstrate combining nullish coalescing and OR let a = null; let b = ""; let c = "Guest"; let result = (a ?? b) || c; console.log(result);
Guest
Parentheses make the evaluation order explicit.
When to Use || and ??
Use || when you want a fallback for any falsy value.
For example:
let name = userInput || "Guest";
This is useful when an empty string should also trigger the default value.
Use ?? when you want a fallback only when a value is missing, represented by null or undefined.
For example:
let count = userCount ?? 0;
This is useful when 0 is a meaningful value that should be preserved.
Advantages of || and ??
- Provide Default Values: Both operators make it easy to provide fallback values.
- Reduce Conditional Code: They can replace simple if-else checks.
- Improve Readability: Short expressions can make default-value logic easier to understand.
- Handle Missing Data: The ?? operator is particularly useful when working with null and undefined.
- Preserve Valid Falsy Values: The ?? operator keeps values such as 0, false, and "".
- Useful with Optional Chaining: ?? works well with ?. when accessing optional properties.
Common Beginner Mistakes
- Assuming || and ?? Are the Same: They differ because || checks all falsy values, while ?? checks only null and undefined.
- Forgetting That 0 Is Falsy: The || operator replaces 0 with the fallback value, while ?? preserves it.
- Forgetting That false Is Falsy: The || operator replaces false, while ?? preserves it.
- Using || When an Empty String Is Valid: If "" is a meaningful value, ?? may be more appropriate.
- Mixing ?? with || or && Without Parentheses: JavaScript requires parentheses when these operators are combined directly.
Conclusion
The || and ?? operators are both useful for providing fallback values in JavaScript, but they are designed for different situations. The || operator uses the fallback whenever the left-side value is falsy, including 0, false, and "". The ?? operator uses the fallback only when the left-side value is null or undefined. Therefore, || is useful when falsy values should be treated as missing, while ?? is better when valid values such as 0, false, and empty strings need to be preserved.
FAQs
1. What is the main difference between || and ?? in JavaScript?2. Does ?? replace 0 with a default value?The || operator returns the right-side value when the left-side value is falsy, while ?? returns the right-side value only when the left-side value is null or undefined.
3. Does || replace an empty string?No. The ?? operator preserves 0 because 0 is not null or undefined.
4. When should I use ?? instead of ||?Yes. An empty string is falsy, so || returns the right-side fallback value.
5. Can ?? be used with || and &&?Use ?? when values such as 0, false, or "" are valid and should be preserved, and you only want a fallback for null or undefined.
Yes, but they cannot be directly mixed without parentheses. Parentheses must be used to explicitly define the intended evaluation order.
0 Comments