Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "{} == {}" result in a SyntaxError? [duplicate]

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?

like image 294
MaX Avatar asked Oct 22 '25 01:10

MaX


2 Answers

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.

like image 84
Alex Wayne Avatar answered Oct 23 '25 17:10

Alex Wayne


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

({}) === ({})

Or:

({} === {})
like image 23
gilly3 Avatar answered Oct 23 '25 17:10

gilly3