15

The following will show in Firebug or in jsconsole.com or in other Javascript interactive console:

>>> foo = { a : 1, b : 2.2 }
Object { a=1, more...}

>>> foo.a
1

>>> foo.b
2.2

>>> { a : 1, b : 2.2 }
SyntaxError: invalid label { message="invalid label", more...}

>>> { a : 1 }
1

why is the 1 returning for {a : 1} and why is {a : 1, b : 2.2} giving an error? In Ruby, they would come back the same way you defined it.

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 1
    This is a great question followed by a terrific answer! Obviously I'm talking about CMS's answer. – Marcus Stade Sep 17 '10 at 00:28
  • http://stackoverflow.com/questions/1509535/javascript-false-and-false/1509664#1509664 is a seemingly irrelevant question, but with a relevant answer to one thing you may be tripping on. – Crescent Fresh Sep 17 '10 at 00:31

3 Answers3

25

The second line is giving you a SyntaxError because the { token at the beginning of it causes an ambiguity, the parser treats it as if it were a Block statement, not the start of an object literal.

For example, a valid Block statement:

{ foo: 'bar' }

The above looks like an object literal, but it isn't, because the code is evaluated in statement context.

It will be parsed as a Block, that contains a labelled statement (foo), followed by an expression statement ('bar').

To ensure that you are using the grammar of an object literal, you can wrap it with parentheses (also known as the grouping operator):

({ foo: 'bar' })

The grouping operator can only take Expressions, therefore there is no ambiguity.

See also:

Community
  • 1
  • 1
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • +1 I learned something new today. I've never put just object-literal syntax into a REPL without assigning it to a var, but this is interesting to know .. – Matt Sep 17 '10 at 00:23
  • (a ‘labelled statement’ is something JavaScript inherited from C, where it can be used for `goto`​s. Labelled statements are almost never used in JavaScript, which is why it comes as a surprise that `{foo: 'bar'}` can be taken as a valid statement.) – bobince Sep 17 '10 at 00:25
0

I'm not 100% positive, but what I think is happening is that in the second line you're defining a block, not an object. Thus the parse error comes when the parser reaches the comma, since it expects a semi color. The labels defned are labels, like in a goto or switch statement. I hope this explanation makes any sense.

Marcus Stade
  • 4,724
  • 3
  • 33
  • 54
-1

console do as eval('you input')

eval({....}) --- this will get an error
eval('({....})')---eval string as a function
sjngm
  • 12,423
  • 14
  • 84
  • 114
  • Were you trying to show the distinction between `eval("{}")` and `eval("({})")` and accidentally forgot the quotes? The descriptions of each case are incorrect, if you fix this. If not, this answer is unrelated to the question. The answer also doesn’t explain why one syntax is incorrect vs. the other and how `eval` is related to any of this. – Sebastian Simon Jul 02 '20 at 14:54