My journey began with the attempt to create an object like this using JSON.parse():
a = {x:1}
// -> Object {x: 1}
Instinctively, I initially tried:
a = JSON.parse('{x:1}')
// -> Uncaught SyntaxError: Unexpected token x
After some trial and error, I discovered:
a = JSON.parse('{"x":1}')
// -> Object {x: 1}
However, when I mistakenly altered the syntax, confusion ensued:
a = JSON.parse("{'x':1}")
//-> Uncaught SyntaxError: Unexpected token '
This led me to question why
- the necessity of quoting property names in JSON.parse()
- the inconsistency in accepting single quotes but rejecting double quotes in the implementation