javascript之 == vs ===
程序员文章站
2022-06-10 09:24:57
...
一、Comparison Overview
1. Speed Compare(运行速度比较):
=== will never be slower than ==.
They both do type checking, so === doesn't do anything extra compared to ==,
but the type check may allow === to exit sooner when types are not the same.
2. How do they work(它们是如何工作的?):
=== :
compare both type and value
== :
only compare value.
if there are different types, then convert the value.
Example:
Exception:
二、How they work?
1、How "===" compare values?
"Type Checking" is the first step !
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is not Number, go to step 11.
5. If x is NaN, return false.
6. If y is NaN, return false.
7. If x is the same number value as y, return true.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.
http://*.com/a/1813267/2893073
2. When use "==", how is the converstion?(不同类型的值如何转换?)
三. Summary(总结)
Always use 3 equals unless you have a good reason to use 2.
Reference:
http://*.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons
http://dorey.github.io/JavaScript-Equality-Table/
-
1. Speed Compare(运行速度比较):
=== will never be slower than ==.
They both do type checking, so === doesn't do anything extra compared to ==,
but the type check may allow === to exit sooner when types are not the same.
2. How do they work(它们是如何工作的?):
=== :
compare both type and value
== :
only compare value.
if there are different types, then convert the value.
Example:
if(true == 1); //true, because 'true' is converted to 1 and then compared if(true === 1); // false if("4" == 4); //true, because "4" is converted to 4 and then compared if("4" === 4); // false if(true == 2); //false, because 'true' is converted to 1
Exception:
var a = new String("abc"); var b = "abc"; if(a==b); // true if(a===b); // false, because typeof a is object, type of b is String
二、How they work?
1、How "===" compare values?
"Type Checking" is the first step !
引用
11.9.6 The Strict Equality Comparison Algorithm
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
1. If Type(x) is different from Type(y), return false.
2. If Type(x) is Undefined, return true.
3. If Type(x) is Null, return true.
4. If Type(x) is not Number, go to step 11.
5. If x is NaN, return false.
6. If y is NaN, return false.
7. If x is the same number value as y, return true.
8. If x is +0 and y is −0, return true.
9. If x is −0 and y is +0, return true.
10. Return false.
11. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
12. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false.
http://*.com/a/1813267/2893073
2. When use "==", how is the converstion?(不同类型的值如何转换?)
三. Summary(总结)
Always use 3 equals unless you have a good reason to use 2.
Reference:
http://*.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons
http://dorey.github.io/JavaScript-Equality-Table/
-