Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does comparing {} and [] show an error? [duplicate]

On my free time, I was just playing with js console, I got an unexpected error:

js> [] == {}
false

js> {} == []
typein:5: SyntaxError: syntax error:

I tried with ===:

js> [] === {}
false
js> {} === []
typein:9: SyntaxError: syntax error:

Am thinking wrong here?

I tested this with Firefox, Chrome and jscore.

like image 872
Renjith Thankachan Avatar asked Feb 12 '17 07:02

Renjith Thankachan


People also ask

What is the meaning of {} in JavaScript?

So, what is the meaning of {} in JavaScript? In JavaScript, we use {} braces for creating an empty object. You can think of this as the basis for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array.

How do you use objects?

Object.is() method is used to determine whether two values are the same or not. Two values can be same if they hold one of the following properties: If both the values are undefined. If both the values are null.


1 Answers

That's because in the second case, {} is interpreted as a code block, rather than an object.

If you try ({}) == [] it works just fine.

js> ({}) == []
false
js> ({}) === []
false
like image 167
JCOC611 Avatar answered Oct 22 '22 22:10

JCOC611