Why does JavaScript’s eval need parentheses to eval JSON data?

eval accepts a sequence of Javascript statements. The Javascript parser
interprets the ‘{’ token, occuring within a statement as the start of a block and not the start of an object literal.

When you enclose your literal into parentheses like this: ({ data_from_the_wire })
you are switching the Javascript parser into expression parsing mode. The token ‘{’ inside an expression means the start of an object literal declaration and not a block, and thus Javascript accepts it as an object literal.

Leave a Comment