In IE and Chrome, typing this into the JavaScript console throws an exception:
{} == false // "SyntaxError: Unexpected token =="
However, all of these statements are evaluated with no problem:
false == {} // false
({} == false) // false
var a = {};
a == false // false
Is this intentional behavior? Why does this happen?
[] == false
is throwing me, but I feel like I've seen it before. Edit: Aha! The array'stoString
method is secretly called, producing''
, which is falsey! Correspondingly,[] == ''
istrue
. – Sitnikfunction a() { b: 1 }
NOT throw an exception? – Sinewfunction a() { b: 1 }
the statement b: 1 is defining a labelb
. See this answer: https://mcmap.net/q/127851/-what-does-39-39-colon-do-in-javascript and the EcmaScript spec section 12.12 – Vally{b: 1} == false
also throws an exception. I guess they have to get rid of ambiguity in the grammar one way or another... – Swanee{ }
in torazaburo'sfunction
definition is not acting like the{ }
in your{b: 1} == false
. His is defining the function body block, whereas yours are the same as{} == false
(a generic code block rather than an object). In your example, remember that (as Benjamin Wegman notes)b: 1
is creating a label in your generic code block, and not an object property. – Sitnik