ankam sarav ankam sarav - 9 months ago 43
Javascript Question

why foo: { bar: 10} prints 10 when executed in chrome console

I'm unable to figure out how

foo: {bar: 10}
when executed in console gives "10".

Please help me understand. Executed this code in chrome console.

Answer Source

foo: is a label. It does nothing since it isn't labeling anything useful.

{} is a block. It groups some statements, but (in this example) not in a significant way. (Important: It is a block, not an object literal.)

bar: is another label. Ditto for foo:.

10 is a Number.

Since everything else does nothing significant, you are basically evaluating 10 so you get 10 as the result.