JavaScript Comparison Operators
Comparison operators 比较 两个值,并返回一个 boolean 值(true
或 false
)。例如,
const a = 3, b = 2;
console.log(a > b);
// Output: true
在这里,我们使用 >
比较运算符来检查 a(值为 3)是否大于 b(值为 2)。
因为 3 大于 2,所以我们得到 true
作为输出。
注意:在上面的例子中,a > b
被称为 boolean 表达式,因为它求值后会得到一个 boolean 值。
常用比较运算符
运算符 | 含义 | 示例 |
---|---|---|
== |
等于 | 3 == 5 // false |
!= |
不等于 | 3 != 4 // true |
=== |
严格等于 | 3 === "3" // false |
!== |
严格不等于 | 3 !== "3" // true |
> |
大于 | 4 > 4 // false |
< |
小于 | 3 < 3 // false |
>= |
大于或等于 | 4 >= 4 // true |
<= |
小于或等于 | 3 <= 3 // true |
1. JavaScript 等于运算符
等于运算符 ==
的求值结果为
true
,如果操作数的值相等。false
,如果操作数的值不相等。
例如,
// same value, same type
console.log(5 == 5); // true
// same value, different type
console.log(2 == "2"); // true
// different values, same type
console.log("hello" == "Hello"); // false
注意:在 JavaScript 中,==
是比较运算符,而 =
是赋值运算符。如果您错误地使用 =
而不是 ==
,可能会得到意想不到的结果。
2. 不等于运算符
不等于运算符 !=
的求值结果为
true
,如果操作数的值不相等。false
,如果操作数的值相等。
例如,
// same value, same type
console.log(2 != 2); // false
// same value, different type
console.log(2 != "2"); // false
// different value, same type
console.log(2 != 3); // true
3. 严格等于运算符
严格等于运算符 ===
的求值结果为
true
,如果操作数的值和类型都相同。false
,如果操作数的值或类型不相同。
例如,
// same value, same type
console.log(2 === 2); // true
// same value, different type
console.log(2 === "2"); // false
==
和 ===
运算符之间的区别。==
(相等)运算符仅检查操作数的值而不检查它们的类型。例如,
然而,===
(严格相等)运算符同时检查操作数的值和类型。例如,
// only checks the values
console.log(2 == "2"); // true
// checks both the values and the types
console.log(2 === "2"); // false
这意味着,只要操作数的值相等,==
运算符就返回 true
。但是,只有当操作数的值和类型都相等时,===
运算符才返回 true
。
4. 严格不等于运算符
严格不等于运算符 !==
的求值结果为
true
,如果操作数的值或类型不相同。false
,如果操作数的值和类型都相同。
例如,
// same value, same type
console.log(2 !== 2); // false
// same value, different type
console.log(2 !== "2"); // true
// different value, same type
console.log("Hello" !== "World"); // true
5. 大于运算符
大于运算符 >
的返回值
true
,如果左侧的值大于右侧的值。false
,如果左侧的值不大于右侧的值。
例如,
// left operand is greater
console.log(3 > 2); // true
// both operands are equal
console.log(4 > 4); // false
// left operand is smaller
console.log(2 > 5); // false
6. 大于或等于运算符
大于或等于运算符 >=
的返回值
true
,如果左侧的值大于或等于右侧的值。false
,如果左侧的值小于右侧的值。
例如,
// left operand is greater
console.log(3 >= 2); // true
// both operands are equal
console.log(4 >= 4); // true
// left operand is smaller
console.log(2 >= 5); // false
7. 小于运算符
小于运算符 <
的返回值
true
,如果左侧的值小于右侧的值。false
,如果左侧的值不小于右侧的值。
例如,
// left operand is smaller
console.log(2 < 5); // true
// both operands are equal
console.log(4 < 4); // false
// left operand is greater
console.log(3 < 2); // false
8. 小于或等于运算符
小于或等于运算符 <=
的返回值
true
,如果左侧的值小于或等于右侧的值。false
,如果左侧的值大于右侧的值。
例如,
// left operand is smaller
console.log(2 <= 5); // true
// both operands are equal
console.log(4 <= 4); // true
// left operand is greater
console.log(3 <= 2); // false
JavaScript 逻辑运算符
逻辑运算符通过求值 boolean 表达式来返回一个 boolean 值。例如,
const x = 5, y = 3;
console.log((x < 6) && (y < 5));
// Output: true
在这里,&&
是逻辑运算符 AND。由于 boolean 表达式 x < 6
和 y < 5
都为 true
,使用 &&
运算符求值它们也得到 true
。
常用逻辑运算符
运算符 | 语法 | 描述 |
---|---|---|
&& (逻辑 AND) |
expression1 && expression2 |
仅当 expression1 和 expression2 都为 true 时为 true |
|| (逻辑 OR) |
expression1 || expression2 |
当 expression1 或 expression2 中至少有一个为 true 时为 true |
! (逻辑 NOT) |
!expression |
如果 expression 为 true 则为 false ,反之亦然 |
1. 逻辑 AND 运算符
逻辑 AND 运算符 &&
当两个表达式都为 true
时返回 true
。例如,
let x = 2;
// both expressions are true
console.log((x < 4) && (4 >= x)); // true
// only one expression is true
console.log((x <= 4) && (2 == 4)); // false
// both expressions are false
console.log((x > 4) && (x == 4)); // false
这里,
(x < 4) && (4 >= x)
的结果是true
,因为两个表达式都为true
。(x <= 4) && (2 == 4)
的结果是false
,因为表达式2 == 4
为false
。(x > 4) && (x == 4)
的结果是false
,因为两个表达式都为false
。
2. 逻辑 OR 运算符
逻辑 OR 运算符 ||
当至少有一个表达式为 true
时返回 true
。例如,
let x = 2;
// both expressions are true
console.log((x < 4) || (4 >= x)); // true
// only one expression is true
console.log((x <= 4) || (2 == 4)); // true
// both expressions are false
console.log((x > 4) || (x == 4)); // false
这里,
(x < 4) || (4 >= x)
的结果是true
,因为两个表达式都为true
。(x <= 4) || (2 == 4)
的结果是true
,因为表达式x <= 4
为true
。(x > 4) || (x == 4)
的结果是false
,因为两个表达式都为false
。
3. 逻辑 NOT 运算符
逻辑 NOT 运算符 !
当指定表达式为 false
时返回 true
,反之亦然。例如,
// NOT on true
console.log(!true); // false
// NOT on false
console.log(!false); // true
// comparison example
console.log(!(2 < 3)); // false
这里,
!true
的结果是false
,因为!
将true
的值反转为false
。!false
的结果是true
,因为!
将false
的值反转为true
。!(2 < 3)
的结果是false
,因为!
将(2 < 3)
的true
值反转为false
。
常见问题
在 JavaScript 中,我们使用比较运算符来比较两个值并获得布尔结果(true
或 false
)。例如,
// less than operator
console.log(4 < 5);
// Output: true
在上面的例子中,我们使用 <
运算符来获取条件 4 < 5
的布尔值。
另一方面,我们使用逻辑运算符对 boolean 表达式执行逻辑运算。例如,
// ! logical NOT
console.log(!(4 < 5));
// Output: false
这里,表达式 4 < 5
为我们提供了布尔值 true
。然后 !
运算符作用于此布尔值并将其反转为 false
。
另请阅读