Source:https://github.com/getify/you-dont-know-js/blob/master/up%20&%20going/ch2.md#equality
False Values in JS
""(empty string)
0, -0 , NaN (invalid number )
null,undefined
Simple Rules Using = =, = = =
- If either value (aka side) in a comparison could
true false is the or value, avoid and use == === .
- If either value in a comparison could is of these specific values (
0 , "" , or [] --empty array), avoid == and Use === .
- In All other cases, you ' re safe to use
== . Not only was it safe, but in many cases it simplifies your code in a by that improves readability.
Object (including function, array) comparsion
Because Those values is actually held by reference, both == and === comparisons would simply check whether the refer Ences match, not anything about the underlying values.
arrayS is by default coerced to string s by simply joining all the values with commas ( , ) in between.
var a = [n/a]; var b = [n/a]; var c = "All-in-all" = = C; // trueb = = C; // trueA = = B; // false
Inequality
stringValues can also is compared for inequality, using typical alphabetic rules ( "bar" < "foo" )
var a = all; var b = "a"; var c = "$"< b; // trueb < C; // true
- If both values in the
< comparison string is S, as it's with b < c , the comparison is made lexicographically.
- But if one or both was not a
string , as it was with a < b , then both values was coerced to be number s, and a typical numeri C comparison occurs.
var a =; var b = "foo"< b; // falsea > b; // falseA = = B; // false
bValue is being coerced to the ' Invalid number value ' in the and NaN < > comparisons.
NaNis neither Greater-than nor Less-than, equal-to any and value. Hence, it returns false.
Source:http://www.ecma-international.org/ecma-262/5.1/
The comparison x = = y , where and is x y values, produces true or false. Such A comparison is performed as follows:
- If Type (x) is the same as Type (y), then
- If Type (x) is Undefined, return true.
- If Type (x) is Null, return true.
- If type ( x ) is number, then
- if x is nan , return false .
- if y is NaN , return false .
- if x is the same number value as y , return true .
- if x is +0 and y is −0 , return true .
- if x is −0 and y is +0 , return true .
- return false .
- If Type (x) is a String, then return true if x and y are exactly the same sequence of Cha Racters (same length and same characters in corresponding positions). Otherwise, return false.
- If Type (x) is a Boolean, return true if x and y are both true or both fals E. Otherwise, return false.
- Return True if x and y refer to the same object. Otherwise, return false.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type (x) is number and Type (y) is a String,
Return the result of the comparison x = = Tonumber (y).
- If Type (x) is a String and Type (y) is number,
Return the result of the comparison tonumber (x) = = y.
- If Type (x) is a Boolean, return the result of the comparison tonumber (x) = = y.
- If Type (y) is a Boolean, return the result of the comparison x = = Tonumber (y).
- If Type (x) is either a String or number and Type (y) is Object,
Return the result of the comparison x = = toprimitive (y).
- If Type (x) is an Object and Type (y) is either String or number,
Return the result of the comparison toprimitive (x) = = y.
- Return false.
The comparison x = = = y , where and x y is values, produces true or false. Such A comparison is performed as follows:
- If type (x) is different from Type (y), return false.
- If Type (x) is Undefined, return true.
- If Type (x) is Null, return true.
- If Type (x) is number and then
- If x is NaN, return false.
- If y is NaN, return false.
- If x is the same number value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- If Type (x) is a String, then return true if x and y are exactly the same sequence of Cha Racters (same length and same characters in corresponding positions); Otherwise, return false.
- If Type (x) is a Boolean, return true if x and y are both true or both fals e; Otherwise, return false.
- Return True if x and y refer to the same object. Otherwise, return false.
Comparsion in JavaScript