That definitely looks like a bug.
From the ECMAScript 5.1 specification:
Conforming implementations of JSON.parse and JSON.stringify must support the exact interchange format described in this specification without any deletions or extensions to the format. This differs from RFC 4627 which permits a JSON parser to accept non-JSON forms and extensions.
And:
JSON.stringify produces a String that conforms to the following JSON grammar. JSON.parse accepts a String that conforms to the JSON grammar
It may be that it somehow wraps the string in a "JSONText" type object which still has a typeof
of string
but that seems very odd.
I would definitely think that the following implementation in this case is the correct one:
JSON.stringify(2) == "2" && JSON.stringify(2) === "2" && JSON.stringify(2) == 2 && JSON.stringify(2) !== 2;
true
According to @6502 (see comment) this is true
in:
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
== 2
yieldstrue
... – ArchaeologicalJSON.stringify(2) == 2
– Pathex = JSON.stringify(2)
, thenx.toString()
,String(x)
,x + ""
etc. all returnfalse
. Weird...x - 0 + "" == "2"
does returntrue
, as doesx + "1" == "21"
. – Wanettawanfried0
to9
. – Molinanew String("2") !== "2"
. – Overcautious(JSON.stringify(2) + '') == "2"
would fix it. – Evansvar js = JSON.stringify; JSON.stringify = function(input) { return js(input).substr(); };
` – Selfstarter