The issue stems from the grammar and parsing context.
When you have {"aaa":"bbb"}
as your code, it falls under a Block [statement] where "aaa" is considered a String followed by a colon, resulting in Invalid Syntax. The problem can be simplified to "aaa":"bbb"
, with the braces adding unnecessary confusion.
On the other hand, when using {aaa:"bbb"}
in your program, it functions as a statement where aaa
(an Identifier) acts as a Label before the string "bbb"
. Although this format is acceptable, it does not generate an object. This format is also equivalent to aaa:"bbb"
within a statement context.
Now, if you use aaa={"aaa":"bbb"}
in your code, the {..}
is interpreted in an expression context as an Object Literal; the resulting object is then assigned to the variable. Other grammar constructs like +{"aaa":"bbb"}
, ({"aaa":"bbb"})
, or even console.log({"aaa":"bbb"})
can force an expression context as well.
In summary, the JavaScript Object Literal syntax may not always apply, highlighting that JSON is not a perfect subset of JavaScript Object Literals. It is advisable to utilize appropriate JSON tools for validation and accuracy.