包阅导读总结
1. 关键词:JavaScript、逻辑或 (||)、空值合并 (??)、默认值、行为差异
2. 总结:本文介绍了 JavaScript 中的逻辑或 (||) 和空值合并 (??) 运算符,阐述了它们的工作原理、返回右侧操作数的情况及差异,说明了何时使用,并通过实用案例展示,强调理解其行为差异对编写清晰高效代码的重要性。
3. 主要内容:
– 理解运算符
– 逻辑或 (||) 运算符
– 工作原理:结合布尔值,处理非布尔值时返回首个真值,全假值返回最后值,在特定左操作数时返回右操作数。
– 示例。
– 空值合并 (??) 运算符
– 工作原理:检查值是否为 null 或 undefined,是则返回右操作数,否则返回左操作数。
– 示例。
– 逻辑或 (||) 和空值合并 (??) 的关键差异
– 何时使用:逻辑或用于任何假值,空值合并用于 null 或 undefined。
– 总结表。
– 实际用例
– 逻辑或 (||) 场景:默认值、短路条件逻辑、处理可选属性和可选链的默认值。
– 空值合并 (??) 场景:处理 null 或 undefined、与可选链结合。
– 结论
– 逻辑或和空值合并是提供默认值的工具,理解差异很关键。逻辑或适用范围广,空值合并更针对 null 或 undefined。
思维导图:
文章地址:https://www.javacodegeeks.com/2024/08/unmasking-the-mystery-vs-in-javascript.html
文章来源:javacodegeeks.com
作者:Eleftheria Drosopoulou
发布时间:2024/8/14 12:52
语言:英文
总字数:923字
预计阅读时间:4分钟
评分:86分
标签:JavaScript,JavaScript 基础,逻辑或运算符,空值合并运算符,默认值
以下为原文内容
本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com
JavaScript offers two powerful tools for providing default values: the logical OR (||) and the nullish coalescing (??) operators. While they might seem similar at first glance, understanding their distinct behaviors is crucial for writing clean, efficient, and predictable code.
In this guide, we’ll dive into the nuances of these operators, exploring when to use each one and why. Let’s start by understanding the basics.
1. Understanding the Operators
The Logical OR (||) Operator
How it works
The logical OR operator (||) is used to combine two boolean values. It returns true
if at least one of the values is true
. However, in JavaScript, it has a broader use: it can be used with any data type.
When used in expressions with non-boolean values, the logical OR operator returns the first truthy value it encounters. If all values are falsy, it returns the last value.
When it returns the right-hand operand
The logical OR operator will return the right-hand operand in the following cases:
- The left-hand operand is
false
. - The left-hand operand is
null
. - The left-hand operand is
undefined
. - The left-hand operand is
0
. - The left-hand operand is an empty string (“”).
- The left-hand operand is
NaN
(Not a Number).
Examples
let x = 0;let y = "hello";let z = null;console.log(x || "default"); // Output: "default" (x is falsy)console.log(y || "default"); // Output: "hello" (y is truthy)console.log(z || "default"); // Output: "default" (z is null, which is falsy)
The Nullish Coalescing (??) Operator
How it works
The nullish coalescing operator (??) is a newer addition to JavaScript. It provides a way to check if a value is either null
or undefined
. If it is, it returns the right-hand operand. Otherwise, it returns the left-hand operand.
When it returns the right-hand operand
The nullish coalescing operator will only return the right-hand operand if the left-hand operand is:
Examples
let x = 0;let y = null;let z = "hello";console.log(x ?? "default"); // Output: 0 (x is not null or undefined)console.log(y ?? "default"); // Output: "default" (y is null)console.log(z ?? "default"); // Output: "hello" (z is not null or undefined)
While the logical OR operator considers many values as falsy, the nullish coalescing operator only checks for null
or undefined
. This makes it more specific in its behavior.
2. Key Differences Between || and ??
When to Use Which Operator
While both the logical OR (||) and nullish coalescing (??) operators can provide default values, their behaviors differ significantly.
Logical OR (||)
- Use when: You want to provide a default value for any falsy value (including
0
,''
,false
). - Example:
let age = 0;let defaultAge = 18;let finalAge = age || defaultAge; // finalAge will be 18
Nullish Coalescing (??)
- Use when: You specifically want to handle only
null
orundefined
values. - Example:
let user = null;let defaultUser = { name: 'Guest' };let finalUser = user ?? defaultUser; // finalUser will be { name: 'Guest' }
Summary Table
Operator | Returns right-hand operand when | Example |
---|---|---|
Left-hand operand is falsy (null, undefined, 0, ”, false, NaN) | ||
?? | Left-hand operand is null or undefined | let value = x ?? 'default'; |
Remember: The key difference lies in how they treat falsy values. ||
considers many values as falsy, while ??
is more specific, only checking for null
or undefined
.
3. Practical Use Cases
Logical OR (||)
Scenario: Default values for optional parameters
function greet(name) { console.log(`Hello, ${name || 'stranger'}!`);}greet('Alice'); // Output: Hello, Alice!greet(); // Output: Hello, stranger!
Scenario: Short-circuiting for conditional logic
const user = { name: 'Bob' };const greeting = user && user.name ? `Hello, ${user.name}!` : 'Hello!';console.log(greeting); // Output: Hello,Scenario: Short-circuiting for conditional logicBob!
Best practices:
- Use
||
when you want to provide a default value for any falsy value. - Be aware of potential side effects if the right-hand operand has side effects.
- Consider using nullish coalescing (??) for more specific null or undefined checks.
Scenario: Handling optional properties
const user = { name: 'Charlie' };const fullName = user?.name ?? 'Unknown';console.log(fullName); // Output: Charlieconst emptyUser = {};const emptyUserName = emptyUser?.name ?? 'Unknown';console.log(emptyUserName); // Output: Unknown
Scenario: Default values for optional chaining
const address = { street: '123 Main St' };const street = address?.street ?? 'No street information';console.log(street); // Output: 123 Main Stconst noAddress = null;const noStreet = noAddress?.street ?? 'No address';console.log(noStreet); // Output: No address
Best practices:
- Use
??
when you specifically want to handlenull
orundefined
values. - Combine with optional chaining (?.) for safer property access.
- Avoid unnecessary nesting of
??
operators.
Additional considerations:
- For more complex logic involving multiple conditions, consider using ternary operators or if-else statements.
- Always test your code thoroughly to ensure the desired behavior.
4. Conclusion
The logical OR (||) and nullish coalescing (??) operators are valuable tools in a JavaScript developer’s arsenal for providing default values. While they might seem similar, understanding their distinct behaviors is crucial for writing clear, efficient, and predictable code.
The logical OR operator is versatile and can handle various falsy values, making it suitable for providing defaults in a broad range of scenarios. However, its behavior with falsy values other than null
or undefined
can sometimes lead to unexpected results.
The nullish coalescing operator, on the other hand, is more specific, focusing solely on null
or undefined
values. This makes it ideal for handling optional properties and avoiding unintended consequences with other falsy values.