4

Possible Duplicate:
Why {} != ( {} ) in JavaScript?

I tried it today and it threw me SyntaxError today and it made me wonder; what's wrong with this?

{} === {}
{} == {}

What's wrong?

Community
  • 1
  • 1
MaX
  • 1,324
  • 13
  • 25

2 Answers2

10

When { is the first token in a line, it's considered the start of block.

{
  some();
  statements();
  here();
}

And not an object literal. A block of code cannot be equal to anything, it's not an assignable thing.

({}) === {}

That will set the parser straight.

Alex Wayne
  • 162,909
  • 46
  • 287
  • 312
  • Nit: "When `{` is the first token on a line.." is a gross approximation. Consider either `;(\n{} == {})` or `var x = \n{};` as counter-examples. –  Jan 26 '13 at 01:43
4

Use parens. Parentheses turn the ambiguous code into an expression:

({}) === ({})

Or:

({} === {})
gilly3
  • 83,908
  • 25
  • 139
  • 172