JavaScript 中的感叹号运算符
本教程将介绍 JavaScript 中逻辑非(!
)运算符的行为和使用。
这 !
运算符将操作数转换为布尔值并返回转换后的值的倒数。
语法
inverse = !expression
当我们应用!
运算符到假值的时候,我们得到 true
作为结果。虚假值包括 - false
、0
、-0
、0n
、""
、null
、undefined
和 NaN
。
当我们应用!
运算符到真值,我们得到假
作为结果。除了提到的虚假值之外,所有值都是真实值。
示例 1
// boolean values
console.log('!true =>', !true); // returns false
console.log('!false =>', !false); // returns true
输出:
!true => false
!false => true
在上面的代码中,
- 应用
!
运算符为true
值,返回false
。 - 应用
!
运算符为false
值,则返回true
。
示例 2
// string values
console.log('!(\'\') =>', !('')); // returns true
console.log('!(\'truthy_string\') => ', !('truthy_string')); // returns false
输出:
!('') => true
!('truthy_string') => false
在上面的代码中,
- 应用
!
运算符为空字符串(空字符串表示假值),返回true
。 - 应用
!
运算符对非空字符串(非空字符串表示真值),返回false
。
示例 3
// number values
console.log('!0 => ', !0); // returns true
console.log('!100 => ', !100); // returns false
输出:
!0 => true
!100 => false
在上面的代码中,
- 应用
!
运算符到0
(0
表示假值),返回true
。 - 应用
!
运算符到100
(非零值表示truthy
值),返回false
。
示例 4
console.log('!null => ', !null); // true
console.log('!undefined => ', !undefined); // true
console.log('!NaN => ', !NaN); // true
输出:
!null => true
!undefined => true
!NaN => true
我们已经应用了!
运算符为假值。对于所有情况,我们得到了真实的结果。
示例 5
function returnsTruthyValue() {
return 100;
}
function returnsFalsyValue() {
return 0;
}
// apply ! to the return value
console.log('!returnsTruthyValue() =>', !returnsTruthyValue()); // false
console.log('!returnsFalsyValue() =>', !returnsFalsyValue()); // true
输出:
"!returnsTruthyValue() => false
"!returnsFalsyValue() => true
在上面的代码中,我们创建了两个函数。
returnsTruthyValue
函数将返回 100(真实值)。returnsFalsyValue
函数将返回 0(假值)。
当我们在函数调用中应用!
时,!
将应用于函数返回的值。 !returnsTruthyValue()
将适用 !
运算符到 returnsTruthyValue
函数返回的值。所以,
- 对
returnsTruthyValue()
函数应用!
运算符,将返回false
。 - 在
returnsFalsyValue()
函数中使用!
运算符将返回true
。
JavaScript 中的双非运算符!!
!!
运算符将为真值返回 true
,为假值返回 false
。此运算符用于将值转换为布尔值。
语法
!!expression
!!
将执行两个操作。
- 将表达式的值转换为布尔值并对其应用逆运算。
- 同样,逆向应用于该值。
例子
console.log('!!true =>', !!true) // true
console.log('!! 0 =>', !!0) // false
输出:
!!true => true
!! 0 => false
在上面的代码中,
- 将
!!
运算符应用于true
,它将首先将true
转换为false
,然后再次将false
转换为true
。 - 将
!!
运算符应用于0
,它将首先将0
转换为布尔值(0
转换为false
)并对其应用逆运算(现在该值将是true
)。然后再一次,将反转应用于先前反转的值(现在该值将是false
)。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。