There seems to be a mismatch between the common understanding of ==
and what it actually does. To give some background for the issue:
typeof new Number(1); // returns object
typeof new String(1); // returns object
typeof 1; // returns number
Seemingly, both Number
and String
are of object
type. No surprise there. However things get interesting for ==
which should return true
when operands are equal regardless of their type.
According to a somewhat authorative description:
Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects. If this attempt to convert the object fails, a runtime error is generated.
In short, ==
should compare objects by their primitive value. Surprisingly:
var numa = new Number(1);
var numb = new Number(1);
var stri = new String(1);
numa.toString() == stri.toString(); // returns true, as expected
numa.valueOf() == stri.valueOf(); // returns true, as expected
numa == stri; // returns false (?!)
numa == numb; // returns false (?!!!)
numa == numa; // returns true, as expected
var numx = 1;
numa == numx; // returns true (?)
numb == numx; // returns true (?)
stri == numx; // returns true (?)
It appears when both operands are objects, the ==
operator uses neither toString()
nor valueOf()
but something else.
What is the standard definition of object equality for ==
?