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

a 夏天 提交于 2021-02-08 12:20:15

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/42185021/why-does-comparing-and-show-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!