I've found that on Opera 11.50 the expression
JSON.stringify(2)
returns an object for which
typeof
returns"string"
constructor.name
isString
charCodeAt(0)
is 50length
is 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 pare equal to "2" is calling .substr(0)
(for example even adding an empty string still pares different).
I've found that on Opera 11.50 the expression
JSON.stringify(2)
returns an object for which
typeof
returns"string"
constructor.name
isString
charCodeAt(0)
is 50length
is 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 pare equal to "2" is calling .substr(0)
(for example even adding an empty string still pares different).
- 4 If it is so then it is a bug. Minimalistic sample to try would be helpful to say for sure. – c-smile Commented Aug 14, 2011 at 16:42
-
3
Interesting,
== 2
yieldstrue
... – Tomasz Nurkiewicz Commented Aug 14, 2011 at 16:44 - 4 Does it also return false if you use x === "2" ? – Tom Commented Aug 14, 2011 at 16:48
-
2
Another interesting thing is that the bug occurs with and only with numbers
0
to9
. – duri Commented Aug 14, 2011 at 17:05 - 2 How interesting, that I'm not suprised. Opera is crazy! – KARASZI István Commented Aug 14, 2011 at 17:06
1 Answer
Reset to default 7That 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 ment) 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/publications/files/ECMA-ST/Ecma-262.pdf