JavaScript object keys are not quoted strings: they are simply primitive string values.
Key definition in source code using
{ unquotedKey: "some value" }
as an object initializer creates and initializes an object with a key-value pair. The syntax does, however, require unquotedKey
to conform to the rules of JavaScript identifiers.
Key definition in source code using
{ "quoted value!": "some value" }
in a means of creating an object with a key that does not conform to the rules of JavaScript identifiers.
The same restrictions apply when accessing JavaScript property names using shorthand dot notation: foo.bar
works because bar
is an identifier, but foo["was here"]
requires array lookup notation because was here
is not an identifier.
Now enter JSON.stringify
. The stringify
method quotes all property names by default, for the purpose of interchanging a string representation of objects between systems and programming languages. It is a misinterpretation to infer from JSON.stringiy
's behavior that quoted identifier names are somehow better, or the proper way to write an object literal in JavaScript with property names designed for programmatic access.