Source:
False Values in JS
""
(empty string)0
,-0
,NaN
(invalidnumber
)null
,undefined
Simple Rules Using ==, ===
- If either value (aka side) in a comparison could be the
true
orfalse
value, avoid==
and use===
. - If either value in a comparison could be of these specific values (
0
,""
, or[]
-- empty array), avoid==
and use===
. - In all other cases, you're safe to 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.
array
s are by default coerced to string
s 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; // trueb == c; // truea == b; // false
Inequality
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; // trueb < c; // true
- If both values in the
<
comparison arestring
s, as it is withb < c
, the comparison is made lexicographically.
- But if one or both is not a
string
, as it is witha < b
, then both values are coerced to benumber
s, and a typical numeric comparison occurs.
var a = 42;var b = "foo";a < b; // falsea > b; // falsea == 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:
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
-
- If (x) is the same as (y), then
- If (x) is Undefined, return true.
- If (x) is Null, return true.
- If (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 (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.
- If (x) is Boolean, return true if x and y are both true or both false. 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 (x) is Number and (y) is String,return the result of the comparison x == (y).
- If (x) is String and (y) is Number,return the result of the comparison (x) == y.
- If (x) is Boolean, return the result of the comparison (x) == y.
- If (y) is Boolean, return the result of the comparison x == (y).
- If (x) is either String or Number and (y) is Object,return the result of the comparison x == (y).
- If (x) is Object and (y) is either String or Number,return the result of the comparison (x) == y.
- Return false.
- If (x) is the same as (y), then
The comparison x === y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If (x) is different from (y), return false.
- If (x) is Undefined, return true.
- If (x) is Null, return true.
- If (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 (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.
- If (x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
- Return true if x and y refer to the same object. Otherwise, return false.