5.4.1. Equality (==) and Identity (===)
The == and === operators check whether two values are the same, using two
different definitions of sameness. Both operators accept operands of any type,
and both return true if their operands are the same and false if they are
different. The === operator is known as the identity operator, and it checks
whether its two operands are “identical” using a strict definition of
sameness. The == operator is known as the equality operator; it checks whether
its two operands are “equal” using a more relaxed definition of sameness that
allows type conversions.
The following rules determine whether two values are identical according to
the ===
operator:
If the two values have different types, they are not identical.
If both values are numbers and have the same value, they are identical, unless either or both values are
NaN
, in which case they are not identical. TheNaN
value is never identical to any other value, including itself! To check whether a value isNaN
, use the globalisNaN( )
function.If both values are strings and contain exactly the same characters in the same positions, they are identical. If the strings differ in length or content, they are not identical. Note that in some cases, the Unicode standard allows more than one way to encode the same string. For efficiency, however, JavaScript’s string comparison compares strictly on a character-by-character basis, and it assumes that all strings have been converted to a “normalized form” before they are compared. See the
String.localeCompare( )
reference page in Part III for another way to compare strings.If both values are the boolean value
true
or both are the boolean valuefalse
, they are identical.If both values refer to the same object, array, or function, they are identical. If they refer to different objects (or arrays or functions) they are not identical, even if both objects have identical properties or both arrays have identical elements.
If both values are
null
or both values areundefined
, they are identical.
The following rules determine whether two values are equal according to the ==
operator:
If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal.
If the two values do not have the same type, they may still be equal. Use the following rules and type conversions to check for equality:
If one value is
null
and the other isundefined
, they are equal.If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.
If either value is
TRue
, convert it to 1 and try the comparison again. If either value isfalse
, convert it to 0 and try the comparison again.If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its
toString( )
method or itsvalueOf( )
method. The built-in classes of core JavaScript attemptvalueOf( )
conversion beforetoString( )
conversion, except for the Date class, which performstoString( )
conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way.Any other combinations of values are not equal.