0

In javaScript, such situation will happen, I want to know why ?

1 == {}  //false
{} == 1 //throw a Error
Nimitt Shah
  • 4,477
  • 2
  • 10
  • 21
peter roe
  • 21
  • 6
  • 5
    When a statement starts with `{`, it is interpreted by the parser as the beginning of a **block** of statements, not an object initializer expression. – Pointy Nov 19 '21 at 14:20
  • thanks your, but I have another about :`[] == [] //false`and `[] == ![] //true` – peter roe Nov 19 '21 at 14:25
  • See https://felix-kling.de/js-loose-comparison/#%5B%5D==%5B%5D and https://felix-kling.de/js-loose-comparison/#%5B%5D==!%5B%5D . The first case is easy: Two different objects are never equal. The second one is a bit more complex. Check out the links. – Felix Kling Nov 19 '21 at 14:44

1 Answers1

0

It's just a syntactic limitation. ({}) == 1 works just fine.

Luaan
  • 62,244
  • 7
  • 97
  • 116