I’ve found that on Opera 11.50 the expression
JSON.stringify(2)
returns an object for which
typeofreturns"string"constructor.nameisStringcharCodeAt(0)is 50lengthis 1
But still
alert(JSON.stringify(2) == "2")
shows “false” in Opera (and the same happens using ===).
Is this a bug or what?
The only way I found to make it compare equal to “2” is calling .substr(0) (for example even adding an empty string still compares different).
That definitely looks like a bug.
From the ECMAScript 5.1 specification:
And:
It may be that it somehow wraps the string in a “JSONText” type object which still has a
typeofofstringbut that seems very odd.I would definitely think that the following implementation in this case is the correct one:
According to @6502 (see comment) this is
truein:Chrome; Firefox; IE9; iPad Safari; OsX Safari; the N1 Android browser
The ECMAScript 5.1 specification document: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf