“JavaScript double question mark vs or”: Two common operators in JavaScript that often confuse beginners and even experienced programmers are the double question mark (??) and the OR (||) operator.
The JS OR operator ||
uses the right value if left is false, while the double question mark??
uses the right value if left is null
or undefined.
In this article, we will explore these two operators, understand their differences, and learn when to use each one. We’ll also include some frequently asked questions (FAQs) to help you further understand these concepts.
Table of Contents
What is the Double Question Mark (??) Operator?
The double question mark (??) operator, also known as the nullish coalescing operator, is a logical operator that returns the right-hand side operand when the left-hand side operand is null or undefined. Otherwise, it returns the left-hand side operand.
Syntax
let result = leftOperand ?? rightOperand;
Code Example
let name = null;
let displayName = name ?? "Guest";
console.log(displayName); // Output: Guest
In the example above, name is null, so the ?? operator returns “Guest”.
What is the OR (||) Operator?
The OR (||) operator is an older and more commonly known operator in JavaScript. It returns the first truthy value encountered, or the last value if none are true.
In more detail, the OR (||) operator is a logical operator that returns the right-hand side operand when the left-hand side operand is false. In JavaScript, false values include false, 0, “” (empty string), null, undefined, and NaN. Otherwise, it returns the left-hand side operand.
Syntax
let result = leftOperand || rightOperand;
Code Example
let age = 0;
let displayAge = age || 18;
console.log(displayAge); // Output: 18
In this example, age is 0, which is a falsy value, so the || operator returns 18.
JavaScript Double Question Mark vs Or: Key Differences Explained
Understanding the differences between these two operators is crucial for using them correctly. Here are the main differences:
Handling False Values
??
: Only checks fornull
orundefined
.||
: Checks for any false value (false
,0
,""
,null
,undefined
,NaN
).
Use Cases
??
: Use when you want to handlenull
orundefined
specifically.||
: Use when you want to handle all false values.
Table of Differences
Feature | Double Question Mark (??) | OR (||) |
---|---|---|
Introduced in | ECMAScript 2020 (ES11) | ECMAScript 1 |
Checks for | null or undefined | Any false value |
Common Use Case | Default values for null or undefined | Default values for any false values |
Example | let result = value1 ?? value2; | let result = value1 || value2; |
When to Use the Double Question Mark (??) Operator
The ?? operator is ideal when you only want to check for null or undefined values and want to allow other falsy values.
Code Example
let user = { name: "Alice", age: 0 };
let userAge = user.age ?? 18;
console.log(userAge); // Output: 0
In this example, user.age
is 0
, which is a valid value and not null
or undefined
. Therefore, ??
returns 0
.
When to Use the OR (||) Operator
The || operator is useful when you want to provide a default value for any falsy operand.
Code Example
let user = {
name: "Bob",
age: 0
};
let userAge = user.age || 18;
console.log(userAge); // Output: 18
Here, user.age is 0, a false value. Therefore, || returns 18.
JavaScript Double Question Mark vs Or: Practical Examples
Let’s look at some practical examples to understand how we can use both operators better.
Code Example 1: User Input
// JavaScript double question mark vs or
// check differences
let userInput = "";
let message = userInput || "Default message";
console.log(message); // Output: Default message
let secureInput = userInput ?? "Default message";
console.log(secureInput); // Output: ""
Code Example 2: Function Parameters
function greet(name) {
let userName = name ?? "Guest";
console.log("Hello, " + userName);
}
// JavaScript double question mark vs or
greet(null); // Output: Hello, Guest
greet(""); // Output: Hello,
Example 3: Configuration Settings
let config = {
timeout: 0
};
// JavaScript double question mark vs or
// Understand numeric comparisons
let timeoutSetting = config.timeout ?? 1000;
console.log(timeoutSetting); // Output: 0
let fallbackSetting = config.timeout || 1000;
console.log(fallbackSetting); // Output: 1000
Conclusion
JavaScript double question mark vs or: Understanding the differences between the double question mark (??) and OR (||) operators is essential for writing effective JavaScript code. While both operators can provide default values, they serve different purposes and handle different types of values. Use ?? when dealing specifically with null
or undefined
, and use || when you need to handle a broader range of falsy values. With this knowledge, you can make better decisions and write cleaner, more efficient code.
If you have any more questions, feel free to refer to the FAQ section or leave a comment below. Happy coding!
FAQs on JavaScript Double Question Mark vs Or
Can I use ?? with || together?
Yes, you can combine both operators in your code. For example:
let user = null;
let age = 0;
let defaultUser = “Guest”;
let defaultAge = 18;
let currentUser = user ?? defaultUser;
let currentAge = age || defaultAge;
console.log(currentUser); // Output: “Guest”
console.log(currentAge); // Output: 18
In this example, ??
handles null
for user
, and ||
handles the falsy value 0
for age
.
What happens if I use ?? with a non-nullish falsy value?
If you use ?? with a non-nullish falsy value, it will not consider it and will return the original value:
let value = 0;
let defaultValue = 10;
let result = value ?? defaultValue;
console.log(result); // Output: 0
Can I use ?? with multiple values?
Yes, you can chain multiple ?? operators:
let value1 = null;
let value2 = undefined;
let value3 = “Hello”;
let result = value1 ?? value2 ?? value3;
console.log(result); // Output: “Hello”
How does || handle non-boolean values?
The || operator returns the first truthy value it gets or the last value if none are truthy, regardless of whether they are boolean:
let value1 = 0;
let value2 = “Hello”;
let value3 = false;
let result = value1 || value2 || value3;
console.log(result); // Output: “Hello”
Are there performance differences between ?? and ||?
The performance differences between ?? and || are negligible in most use cases. However, using the correct operator based on your needs can make your code clearer and more maintainable.
๐๏ธโโ๏ธ Discover Code Blocks From 20+ yrs JS Expert
๐ฅ Asp.net C# Developer
๐ Solution Architect
๐จโโ๏ธ Database Administrator
๐ข Speaker
๐ MCTS since 2009
Leave a Reply