If I open my browser console (tested in Chrome/Firefox) and type:
null == {}
I get:
false
However, if I commute both arguments to the ==
operator and instead type:
{} == null
I get:
Uncaught SyntaxError: Unexpected token ==
Image:
- Why does this happen?
- Why does this only happen in a console and not when the browser executes a script within an HTML page?
EDIT:
While question 35812626 addresses this and explains the cause as JS parsing the {}
as a code block, it uses the triple equals (strict comparison) operator ===
, not the double equals ==
. As a user points out below, a code block can definitely be followed by ==
without causing a syntax error:
{} == {} // false
How come this works and my example does not?