Operands of the abstract equality operator are compared after being converted to a common type. How this conversion happens is based on the specification of the operator:
Specification for the ==
operator:
7.2.13 Abstract Equality Comparison
The comparisonx == y
, wherex
andy
are values, producestrue
orfalse
. Such a comparison is performed as follows:
- If
Type(x)
is the same asType(y)
, then:
- a. Return the result of performing Strict Equality Comparison
x === y
.
- If
x
isnull
andy
isundefined
, returntrue
.- If
x
isundefined
andy
isnull
, returntrue
.- If
Type(x)
isNumber
andType(y)
isString
, return the result of the comparisonx == ToNumber(y)
.- If
Type(x)
isString
andType(y)
isNumber
, return the result of the comparisonToNumber(x) == y
.- If
Type(x)
isBoolean
, return the result of the comparisonToNumber(x) == y
.- If
Type(y)
isBoolean
, return the result of thecomparison x == ToNumber(y)
.- If
Type(x)
is eitherString
,Number
, orSymbol
andType(y)
isObject
, return the result of the comparisonx == ToPrimitive(y)
.- If
Type(x)
is Object andType(y)
is eitherString
,Number
, orSymbol
, return the result of the comparisonToPrimitive(x) == y
.- Return
false
.
1 == 1; // true
1 == true; // true (operand converted to number: true => 1)
1 == '1'; // true (operand converted to number: '1' => 1 )
1 == '1.00'; // true
1 == '1.00000000001'; // false
1 == '1.00000000000000001'; // true (true due to precision loss)
null == undefined; // true (spec #2)
1 == 2; // false
0 == false; // true
0 == undefined; // false
0 == ""; // true