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, wherexandyare values, producestrueorfalse. 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
xisnullandyisundefined, returntrue.- If
xisundefinedandyisnull, returntrue.- If
Type(x)isNumberandType(y)isString, return the result of the comparisonx == ToNumber(y).- If
Type(x)isStringandType(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, orSymbolandType(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