Why do I get a syntax error if I run this in the console? {} === {}

独自空忆成欢 提交于 2019-12-13 02:59:25

问题


In the chromium console I run {} === {} and I get a Syntax error, unexpected '==='.

If I however wrap this in parens, like ({} === {}) then I get false, what I'd expect.

Is an object literal, in the first position, confused with a code block or something?


回答1:


Without a surrounding parenthesis, {} would be considered as an empty code block in javascript. = followed by a code block would be an invalid syntax. That is why you are seeing an error there.

If you wrap it inside of a parenthesis like ({} === {}), then it would be considered as an expression and it will be evaluated to false as both are referencing two different objects.

The following example may give you a clear picture about it,

{ var x = 5; console.log(x); } == 2
// will throw the same error that you are facing.


来源:https://stackoverflow.com/questions/35812616/why-do-i-get-a-syntax-error-if-i-run-this-in-the-console

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