Javascript 简明教程
JavaScript - Comparison Operators
JavaScript Comparison Operators
JavaScript 中的比较运算符比较两个变量或值,并根据比较结果返回一个布尔值,即 true 或 false。例如,我们可以使用比较运算符检查两个操作数是否相等。
The comparison operators in JavaScript compare two variables or values and return a boolean value, either true or false based on comparison result. For example, we can use the comparison operators to check whether two operands are equal or not.
比较运算符用于逻辑表达式。逻辑表达式被求值为 true 或 false。
The comparison operators are used in logical expressions. A logical expression is evaluated to either true or false.
比较运算符是二元运算符,因为它们对两个操作数执行操作。操作数可以是数值、字符串、逻辑或对象值。
The comparison operators are binary operators as they perform operations on two operands. The operands can be numerical, string, logical, or object values.
JavaScript 中有八个比较运算符来执行不同类型的比较。在这里,我们给出了一个表格,解释了每个比较运算符的示例。
There are eight comparison operators in JavaScript to perform different types of comparison. Here, we have given a table explaining each comparison operator with the example.
Operator |
Description |
Example |
== |
Equal |
x == y |
!= |
Not Equal |
x != y |
=== |
Strict equality (equal value and equal type) |
x === y |
!== |
Strict inequality (not equal value or not equal type) |
x !== y |
> |
Greater than |
x > y |
< |
Less than |
x < y< |
>= |
Greater than or Equal to |
x >= y |
⇐ |
Less than or Equal to |
x ⇐ y |
How comparison is done?
如果两个操作数的类型相同,比较运算符会比较它们的值。然而,如果操作数的类型不同,JavaScript 会对比较执行适当的类型转换。这被称为类型强制转换。
If both operands are of same type, the comparison operators compare the values. However, if the operands are of different types, JavaScript perform appropriate type conversion for the comparison. This is known as type coercion.
如果两个操作数都是数字,将通过检查操作数的数值来进行比较。字符串的比较基于按字母顺序排列,使用 Unicode 值。当字符串与数字比较时,会执行以下类型强制转换。
The comparison is done by checking the numerical values of the operands if both the operands are numbers. The strings are compared based on lexicographical ordering, using Unicode values. The following type coercion is done when a string is compared with a number.
-
If the string contains only numeric value, it is converted to number type.
-
If the string contains non-numeric values as well, it will be converted to NaN.
-
If string is empty, it is converted to zero.
严格相等 (===) 和严格不相等 (!==) 运算符执行严格比较。这些运算符在执行比较操作之前不执行类型转换。
The strict equality (===) and strict inequality (!==) operators perform strict comparison. These operators don’t perform type conversion before performing comparison operation.
Dealing with falsy values
JavaScript 中有一些错误的值。在执行比较时,JavaScript 以不同方式处理这些错误的值。以下是不正确的价值观:
There are some falsy values in JavaScript. JavaScript deals with these falsy values differently while performing the comparison. Followings are the flasy values −
-
0 (zero)
-
false
-
' ' or " " (Empty String)
-
null
-
undefined
-
NaN
所有比较运算符(除了===和!==)在执行比较之前将 false 和空字符串转换为零。
All comparison operators (excepts === and !==) converts false and empty string to zero before performing comparison.
除此之外,小于和大于运算符(<、⇐、>、>=)将 null 转换为零,将 undefined 转换为 NaN。
In addition to above, the less and, greater than operators (<, ⇐, >, >=) convert null to zero and undefined to NaN.
JavaScript Equality (==) Operator
“相等”运算符检查两个操作数的值是否相等。如果操作数相等,则返回 true,否则返回 false。如果操作数的类型不同,它将执行类型转换,然后比较操作数。
The "equality" operator checks if the value of two operands are equal or not. It returns true if the operands are equal, otherwise it returns false. If the operands are of different types, it performs type conversion and then compare the operands.
让我们来看一些没有类型转换的比较示例。两个操作数的类型相同。
Let’s look at some examples of comparison with no type conversion. The both operands are of same type.
const a = 10;
const b = 20;
a == 10; //true
a == b; // false
"Hello" == "Hello"; // true
现在让我们检查一些带类型转换的比较示例。此处的操作数类型不同。
Now let’s check some example of comparison with type conversion. Here the operands are of different types.
5 == '5'; // true
0 == false; // true
0 == ''; // true
在上面的第一个示例中,“5” 转换为 5(字符串到数字转换)。错误和空字符串(' '),在比较之前被转换为零 (0)。
In the first example above, '5' is converted to 5 (string to number conversion). The false and empty string (' '), are converted to zero (0) before comparison.
Example
以下代码展示如何在 JavaScript 中使用相等运算符:
The following code shows how to use equality operator in JavaScript −
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a == b);
document.getElementById("output").innerHTML = "(a == b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
JavaScript Inequality (!=) Operator
“不等”运算符检查两个操作数的值是否不等。如果操作数不等,则返回 true,否则返回 false。与相等运算符相同,如果操作数不是同一种类型,则会执行类型转换。
The "inequality" operator checks if the values of two operands are not equal. It returns true if the operands are not equal, otherwise it returns false. Same as the equality operator, type conversion is performed if the operands are not of same type.
在下面的示例中,比较两个相同类型的变量是否不等。如果值不等,则不等运算符将返回 true。
In the example below two values of same type are compared for inequality check. If the values are not equal, the inequality operator will return true.
10 != 10; // false
10 != 20; // true
"Hello" != "Hello"; // false
让我们检查当操作数类型不同时的不等性
Let’s check for inequality when the operands are of different types
10 != '10'; // false
0 != false; // false
在第一个示例中,将“10”类型转换为 10。此处字符串转换为数字类型。在第二个示例中,将 false(布尔值)转换为零(数字)。
Here in first example, '10' is type casted to 10. Here string is converted to number type. In second example, false (Boolean value) is converted to zero (number).
Example
以下代码展示如何在 JavaScript 中使用不等运算符。
The following code shows how to use inequality operator in JavaScript.
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a != b);
document.getElementById("output").innerHTML = "(a != b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
JavaScript Strict Equality (===) Operator
“严格相等”运算符检查两个操作数的值和数据类型是否相等。如果两个操作数相等并且类型相同,则返回 true。
The "strict equality" operator checks whether the values and data types of the two operands are equal or not. It returns true if both operands are equal and of same type.
换句话说,它在不进行类型转换的情况下检查操作数的相等性。如果操作数的类型不同,则在不进一步检查值的情况下返回 false。
In other words, it checks the equality of the operands without the type conversion. If the operands are of different types, it returns false without further checking the value.
10 === 10; // true
10 === 20; // false
'Hello'==='Hello'; // true
10 === '10'; // false
0 === false; // false
Example
以下代码展示如何在 JavaScript 中使用严格相等运算符。
The following code shows how to use strict equality operator in JavaScript.
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a === b);
document.getElementById("output").innerHTML = "(a === b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
Strict Inequality (!==) Operator
“严格不等”运算符检查两个操作数的值或类型是否不等。如果操作数同类型但不等或类型不同,则返回 true。
The "strict inequality" operator checks whether the two operands are not equal in value or type. It returns true if the operands are of same type but not equal or are of different types.
与严格相等运算符相同,它也首先检查操作数的不相等而不进行类型转换。如果操作数类型不同,则在不进一步检查值的情况下返回 true。
Same as strict equality operator, it also first checks the inequality of operands without type conversion. If the operands are of different type, it will return true without further checking the value.
10 !== 10; //returns false
10 !== 20; // returns true
'Hello'!=='Hello'; // returns false
10 !== '10'; //return true
0 !== false; //returns true
Example
以下代码展示如何在 JavaScript 中使用严格不等运算符。
The following code shows how to use strict inequality operator in JavaScript.
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a !== b);
document.getElementById("output").innerHTML = "(a !== b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
JavaScript Greater Than (>) Operator
“大于”运算符检查左操作数的值是否大于右操作数的值。如果是,则返回 true,否则返回 false。
The "greater than" operator checks if the value of the left operand is greater than the value of the right operand. If yes, it returns true otherwise it returns false.
20 > 10; // true
10 > 10; // false
"ab" > "aa"; // true
10 > '5'; // true
Example
以下代码展示如何在 JavaScript 中使用大于运算符:
The following code shows how to use greater than operator in JavaScript −
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a > b);
document.getElementById("output").innerHTML = "(a > b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
Greater Than or Equal (>=) Operator
“大于或等于”运算符检查左操作数的值是否大于或等于右操作数的值。如果是,则返回 true,否则返回 false。
The "greater than or equal" operator checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, it returns true otherwise false.
10 >= 5; // true
5 >= 5; // true
"ab" >= "aa"; // true
10 >= '5'; // true
Example
以下代码展示如何在 JavaScript 中使用大于或等于运算符:
The following code shows how to use greater than or equal to operator in JavaScript.
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a >= b);
document.getElementById("output").innerHTML = "(a >= b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
JavaScript Less Than (<) Operator
“小于运算符”在左操作数的值小于右操作数的值时返回 true,否则返回 false。
The "less than operator" returns true if the value of the left operand is less than the value of the right operand, otherwise it returns false.
10 < 20; // true
5 < 5; // false
"ab" < "aa"; // true
10 < '5'; // false
Example
以下代码展示如何在 JavaScript 中使用小于运算符:
The following code shows how to use less than operator in JavaScript −
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a < b);
document.getElementById("output").innerHTML = "(a < b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
JavaScript Less Than or Equal (⇐) Operator
小于或等于运算符检查左操作数的值是否小于或等于右操作数的值。如果是,则条件变为 true。
The less than or equal operator checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.
10 <= 20; // true
5 <= 5; // true
"ab" <= "aa"; // false
10 <= '5'; // false
Example
以下代码展示如何在 JavaScript 中使用小于或等于运算符 −
The following code shows how to use less than or equal operator in JavaScript −
<html>
<body>
<div id="output"></div>
<script>
const a = 10;
const b = 20;
let result = (a <= b);
document.getElementById("output").innerHTML = "(a <= b) => " + result;
</script>
<p> Set the variables to different values and then try...</p>
</body>
</html>
Comparing null, undefined and NaN
在 JavaScript,null、undefined 和 NaN 是假值,不会转换为零 (0),而用于比较。
In JavaScript, null, undefined and NaN are the falsy values that are not converted to zero (0) for the comparison.
0 == null; // returns false
0 == undefined; // returns false
0 == NaN; // returns false
null 和 undefined 通常相等。
null and undefined are weekly equal.
null == undefined; // returns true
null === undefined; // returns false
NaN 的类型为数字,但它不等于零。有趣的是,NaN 也不等于 NaN 本身。
The type of NaN is number but it is not equal to zero. Interestingly NaN is not equal to NaN itself.
NaN == NaN; // returns false