Comparison operators

Comparison Operators (比较运算符)

You know comparison operators from math class, but let’s refresh your knowledge:

Greater than operator (a > b) returns true if the left operand is greater than the right operand. (如果左操作数大于右操作数,则大于运算符(a > b)返回true。)

Syntax:

Syntax:

a > b

Example of the greater than operator:

console.log(7 > 4); // true

Less than operator (a < b) returns true if the left operand is less than the right operand.

Syntax:

Syntax:

a < b

Example of the less than operator:

console.log(4 < 7); // true

Greater than or equal operator (a >= b) returns true if the left operand is greater than or equal to the right operand. (如果左操作数大于或等于右操作数,则大于或等于运算符(a > = b)返回true。)

Syntax:

Syntax:

a >= b

Example of the greater than or equal operator:

console.log(7 >= 4); // true
console.log(4 >= 4); // true

Less than or equal operator (a <= b) returns true if the left operand is less than or equal to the right operand.

Syntax:

Syntax:

a <= b

Example of the less than or equal operator:

console.log(4 <= 7); // true
console.log(4 <= 4); // true

The equality operator (a == b) converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, JavaScript compares internal references which are equal when operands refer to the same object in memory. (等式运算符(a = = b)转换操作数,如果它们不是相同的类型,然后应用严格的比较。如果两个操作数都是对象,则JavaScript会在操作数引用内存中的相同对象时比较相等的内部引用。)

Syntax:

Syntax:

a == b

Example of the equality operator:

console.log(1 == 1); // true
console.log("2" == 2); // true
console.log(3 == '3'); // true
console.log(0 == false); // true
console.log(0 == null); // false
console.log(0 == undefined); // false
console.log(null == undefined); // true

If the operands are not equal, inequality operator (!=) returns true. When two operands are not of the same type, JavaScript tries to convert the operands to an appropriate type for the comparison. (如果操作数不相等,则不等式运算符(! =)返回true。当两个操作数类型不同时, JavaScript会尝试将操作数转换为适当的类型进行比较。)

If both operands are objects, JavaScript compares references that are not equal, when operands refer to different objects in memory. (如果两个操作数都是对象,则当操作数引用内存中的不同对象时, JavaScript会比较不相等的引用。)

Syntax:

Syntax:

a != b

Example of the inequality operator:

console.log(1 !=  2) ;    // true
console.log(2 != "2");    // false
console.log(3 != '3' );   // false
console.log(1 != true);   // false
console.log(0 !=  false);  // false

Boolean is the result

Boolean is the result (布尔值是结果)

A comparison returns a value like all other operators. The value, in this case, is a boolean. (比较会像所有其他运算符一样返回一个值。在这种情况下,该值是一个布尔值。)

  • true is “yes”, “correct” or “the truth”;

  • false is “no”, “wrong” or “not the truth”. (- false为“no”、“wrong”或“not the truth”。)

console.log( 3 > 1 );  // true (correct)
console.log( 3 == 1 ); // false (wrong)
console.log( 3 != 1 ); // true (correct)

A comparison result can be assigned to a variable, like any value:

let result = 7 > 4; // the result of the comparison
console.log ( result ); // true

String comparison

String comparison (字符串比较)

JavaScript uses “dictionary” or “lexicographical” order to see if one string is greater than another. In other words, strings are compared letter-by-letter. (JavaScript使用“字典”或“词典编纂”顺序来查看一个字符串是否大于另一个字符串。换句话说,字符串逐个字母进行比较。)

Example of the string comparison:

console.log( 'Z' > 'A' ); // true
console.log( 'Want' > 'Walk' ); // true
console.log( 'Too' > 'To' ); // true

In the examples the comparison ‘Z’ > ‘A’ gets to a result at the first step while the strings “Want” and “Walk” are compared character-by-character:

  • W is the same as W. (- W与W相同。)

  • a is the same as a. (- a与a相同。)

  • n is greater than l. (-n大于l。)

Comparison of different types

Comparison of different types (不同类型的比较)

For comparing values of different types, JavaScript converts the values to numbers. (为了比较不同类型的值, JavaScript会将值转换为数字。)

console.log( '3' > 1 ); // true, string '3' becomes a number 3
console.log( '02' == 2 ); // true, string '02' becomes a number 2

For boolean values, true becomes 1, false becomes 0. (对于布尔值, true变为1 , false变为0。)

console.log( true == 1 ); // true
console.log( false == 0 ); // true

Strict equality

A regular equality check == has a problem, as it can’t differentiate 0 from false:

console.log( 0 == false ); // true

We meet the same thing with an empty string:

console.log( '' == false ); // true

Comparison with null and undefined

Comparison with null and undefined (与null和undefined的比较)

Non-intuitive behavior is when null or undefined are compared to other values. For a strict equality check ===. These values are different, as each of them is a different type. (非直观行为是指将null或undefined与其他值进行比较。 对于严格的平等检查= = =。 这些值是不同的,因为它们都是不同的类型。)

console.log( null === undefined ); // false

For a non-strict check ==

For a non-strict check == (对于非严格检查= =)

These two are a “sweet couple”, it means that they equal each other. (这两个人是一对“甜蜜的夫妇” ,这意味着他们彼此相等。)

console.log( null == undefined ); // true

The Difference between == and ===

The Difference between == and === (= =和= = =之间的区别)

JavaScript has two visually similar, but very different ways to test equality:

== (Double equals operator): the equality or abstract comparison operator

=== (Triple equals operator): the identity or strict comparison operator

Here are the differences between == and ===:

  • before showing comparison == converts the variable values of the same type;

  • === does not do any type conversion and returns true only if both values and types are identical for the two variables being compared. (- = = =不执行任何类型转换,仅当所比较的两个变量的值和类型相同时才返回true。)

var val1 = 13;
var val2 = 13;
console.log(val1 == val2); // true
console.log(val1 === val2); // also true

For maths and other comparisons < > <= >=

For maths and other comparisons < > <= >=

Null/undefined are converted to numbers, here null becomes 0, undefined becomes NaN. (NULL/UNDEFINED转换为数字,这里NULL变为0 , UNDEFINED变为NaN。)

We present you some funny things that happen when we apply these rules. And also, how to not fall into a trap with them. (当我们应用这些规则时,我们会向您展示一些有趣的事情。还有,如何避免与他们陷入陷阱。)

Strange result: null vs 0

Strange result: null vs 0

Example of the compare null with a zero:

console.log( null > 0 );  // (1) false
console.log( null == 0 ); // (2) false
console.log( null >= 0 ); // (3) true

Mathematically, that’s strange. The last result says that “null is greater than or equal to zero”, it means that in one of the comparisons above it must be true, but both of them are false. (在数学上,这很奇怪。最后一个结果是“null大于或等于零” ,这意味着在上面的一个比较中,它必须为真,但它们都是假的。)

The main reason is that an equality check == and comparisons > < >= <= work in a different way: comparisons convert null to a number, treating it as 0. That’s the reason why (3) null >= 0 is true and (1) null > 0 is false.

But on the other hand, the equality check == for undefined and null is defined without any conversions. They equal each other and don’t equal anything else, that’s why (2) null == 0 is false. (但另一方面, undefined和null的相等检查= =在没有任何转换的情况下被定义。它们彼此相等,不等于其他任何东西,这就是为什么( 2 ) null = = 0为false。)

An incomparable undefined

An incomparable undefined (无与伦比的未定义)

We can’t compare the value undefined to other values:

console.log( undefined > 0 ); // false (1)
console.log( undefined < 0 ); // false (2)
console.log( undefined == 0 ); // false (3)

The reasons why we get these results are:

  • Comparisons (1) and (2) return false, as undefined gets converted to NaN, it’s a special numeric value which returns false for all comparisons. (- Comparison (1)和(2)返回false ,当undefined转换为NaN时,它是一个特殊的数值,对于所有比较都返回false。)

  • The equality check (3) returns false, as here undefined only equals null, undefined and no other value. (-相等检查(3)返回false ,因为这里的undefined仅等于null、undefined且没有其他值。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部