标签:
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, undefinedSimple Rules Using ==, ===
true or false value, avoid == and use ===.0, "", or [] -- empty array), avoid == and use ===.==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.Object(including function, array) Comparsion
Because those values are actually held by reference, both == and ===comparisons will simply check whether the references match, not anything about the underlying values.
arrays are by default coerced to strings by simply joining all the values with commas (,) in between.
var a = [1,2,3]; var b = [1,2,3]; var c = "1,2,3"; a == c; // true b == c; // true a == b; // false
string values can also be compared for inequality, using typical alphabetic rules ("bar" < "foo")var a = 41; var b = "42"; var c = "43"; a < b; // true b < c; // true
< comparison are strings, as it is with b < c, the comparison is made lexicographically.string, as it is with a < b, then both values are coerced to be numbers, and a typical numeric comparison occurs.var a = 42; var b = "foo"; a < b; // false a > b; // false a == b; // false
b value is being coerced to the "invalid number value" NaN in the < and > comparisons.NaN is neither greater-than nor less-than, equal-to any other value. Hence, it returns false.Source:http://www.ecma-international.org/ecma-262/5.1/
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
标签:
原文地址:http://www.cnblogs.com/lilixu/p/4612316.html