
When writing JavaScript, it’s common to assign default values to variables in case they are null, undefined, or another falsy value. Two operators that help us do this are the nullish coalescing operator (??) and the logical OR operator (||). While they might look similar, each behaves differently and is suited for different scenarios.

The nullish coalescing operator (??) provides a default value only when the left-hand operand is null or undefined. This is especially useful when you want to allow other "falsy" values, like 0, false, or "", without triggering the fallback.
let result = leftValue ?? rightValue;
Here, result will be assigned leftValue if it’s not null or undefined. Otherwise, it will use rightValue.
let age = 0; let displayAge = age ?? 18; // Result: 0, because 0 is not null or undefined let userName = null; let displayName = userName ?? "Guest"; // Result: "Guest", because userName is null
In the first example, we don’t replace age with 18 because 0 is a valid number, not null or undefined. In the second example, however, userName is null, so we assign "Guest" as the fallback.
The logical OR operator (||) assigns a default value if the left-hand operand is any falsy value. Falsy values in JavaScript include 0, NaN, false, "" (an empty string), null, and undefined.
let result = leftValue || rightValue;
Here, result will be assigned leftValue if it’s truthy (not a falsy value). Otherwise, it will use rightValue.
let age = 0; let displayAge = age || 18; // Result: 18, because 0 is falsy let userName = ""; let displayName = userName || "Guest"; // Result: "Guest", because an empty string is falsy
In both examples, || replaces the falsy values 0 and "" with the provided defaults. Unlike ??, which only checks for null or undefined, || will trigger the fallback for any falsy value.
| Operator | Returns Right Operand When Left Operand Is | Allows Left Operand If It’s |
|---|---|---|
| ?? | null or undefined | Falsy values like 0, "", false, NaN |
| ` | ` |
Summary:
Knowing which operator to use depends on the behavior you want:
If you’re setting a default number, like 0, or a boolean like false, prefer ?? to avoid unintended fallbacks.
let count = 0; let displayCount = count ?? 10; // Keeps 0, rather than using 10
If you want a default for cases where any falsy value should fall back, such as an optional input that might be empty, use ||.
let username = ""; let displayUsername = username || "Anonymous"; // Uses "Anonymous" since username is empty
Both ?? and || are powerful tools for providing default values, but they serve different purposes. ?? is best for cases where only null or undefined should be replaced, preserving other values. || works for broader checks, making it great for generic fallback cases.
Using the right operator can make your code clearer and prevent unexpected behavior—understanding when to use ?? versus || is a simple yet effective way to keep your code efficient and reliable.
Comments
No Comments
Leave a replay
Your email address will not be publish. Required fields are marked *