4

I why does the console in Chrome and Firefox evaluate the current to 1:

> {a:1}
1

I would guess that it would be evaluated as an object like if you assign it to a variable:

> var a = {a:1}
undefined
> a
Object {a: 1}

And with quotes it throws an syntax error:

> {"a":1}
SyntaxError: Unexpected token :
c69
  • 19,951
  • 7
  • 52
  • 82
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123

1 Answers1

8

Try ({a:1}).

Just executing {a:1} is not what you think it is. It is not an object literal, which must be an expression (for example, on the right side of assignment).

Instead, what you have is a block, a label, and then a 1.

{
    a:
    1
}

Blocks return the result of their evaluation, and labels return the result of evaluating the statement that follows the label, so 1 is returned.

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • What would a label be doing there? How would it be use full or would it not? – Andreas Louv Jun 10 '13 at 20:36
  • @NULL [MDN label docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label). – ajp15243 Jun 10 '13 at 20:38
  • 3
    @NULL, per spec, labels can only be used with the `break` and `continue` statements. However, even though you have no way of using the label in this particular context, there's nothing *syntactically* invalid about defining one. It's a peculiarity. – josh3736 Jun 10 '13 at 20:38