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,334
  • 13
  • 26

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
  • 178,991
  • 47
  • 309
  • 337
  • 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
  • 87,962
  • 25
  • 144
  • 176