0

In Firefox console it throws an error because of the colon:

{"a": 1}

SyntaxError: unexpected token: ':'

In Chrome console it accepts it:

{"a": 1}
{a: 1}

I know both accepts {a: 1}, I'd like to know why Firefox chokes on the string key version.

Tom
  • 7,515
  • 7
  • 38
  • 54

1 Answers1

3

See

Why is {} + {} no longer NaN in Chrome console?

Chrome devtools now automatically wrap everything that begins with { and ends with } in an implicit pair of parentheses (see code), to force its evaluation as an expression.

Firefox does not do this. In Firefox,

{a: 1}

is evaluated as a block, which has a label of a, with an unused expression 1:

{
  a:
  1
}

(this is why, in FF, you see that the final expression evaluated is 1:

enter image description here

)

But labels cannot be enclosed in string delimiters (labels need to have the plain identifier only, just like a variable), so changing the a: to "a": throws an error, because colons can only be parsed when after a label, or between a key-value pair in an object.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Interesting.. I often paste value expressions into the firefox console and I always assumed they are parsed as expressions, because it worked fine. Looks like i was just like lucky that they were e.g. wrapped in arrays, so they were evaluated properly: [{"a" : 1}] – Tom Jun 16 '19 at 09:36