转载自Stack Overflow,解释了JS中== 和 === 两个操作符的不同

转载自Stack Overflow,解释了JS中== 和 === 两个操作符的不同
The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

Reference: Javascript Tutorial: Comparison Operators

The == operator will compare for equality after doing any necessary type conversions. The ===operator will not do the conversion, so if two values are not the same type === will simply return false. Both are equally quick.

To quote Douglas Crockford's excellent JavaScript: The Good Parts,

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. the rules by which they do that are complicated and unmemorable. These are some of the interesting cases:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true
​
false == 'false'    // false
false == '0'        // true
​
false == undefined  // false
false == null       // false
null == undefined   // true
​
' \t\r\n ' == 0     // true

The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator.

Update:

A good point was brought up by @Casebash in the comments and in @Phillipe Laybaert's answerconcerning reference types. For reference types == and === act consistently with one another (except in a special case).

var a = [1,2,3];
var b = [1,2,3];
​
var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };
​
var e = "text";
var f = "te" + "xt";
​
a == b            // false
a === b           // false
​
c == d            // false
c === d           // false
​
e == f            // true
e === f           // true

The special case is when you compare a literal with an object that evaluates to the same literal, due to its toString or valueOf method. For example, consider the comparison of a string literal with a string object created by the String constructor.

"abc" == new String("abc")    // true
"abc" === new String("abc")   // false

Here the == operator is checking the values of the two objects and returning true, but the === is seeing that they're not the same type and returning false. Which one is correct? That really depends on what you're trying to compare. My advice is to bypass the question entirely and just don't use the String constructor to create string objects.

=========================================================================
==摘自http://www.c-point.com/javascript_tutorial/jsgrpComparison.htm

JavaScript tutorial:
Comparison operators
Contents | JavaScript Language Reference

The Comparison operators is use to get a Boolean value indicating the result of the comparison.

Syntax
expression1 comparisonoperator expression2

The Comparison operator syntax has these parts:

Part

Description

expression1

Any expression.

comparisonoperator

Any comparison operator.

expression2

Any expression.

Return boolean
Returns a Boolean value indicating the result of the comparison.

Example
When comparing strings, JavaScript uses the Unicode character value of the string expression.

The following describes how the different groups of operators behave depending on the types and values of expression1 and expression2:

Relational (<, >, <=, >=)

Attempt to convert both expression1 and expression2 into numbers.

If both expressions are strings, do a lexicographical string comparison.

If either expression is NaN, return false.

Negative zero equals Positive zero.

Negative Infinity is less than everything including itself.

Positive Infinity is greater than everything including itself.

Equality (==, !=)

If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.

NaN is not equal to anything including itself.

Negative zero equals positive zero.

null equals both null and undefined.

Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.

Every other comparison is considered unequal.

Identity (=. !)

These operators behave identically to the equality operators except no type conversion is done,

posted @ 2019-09-01 21:07  Atituiset  阅读(131)  评论(0)    收藏  举报