by esrauch · 6y ago · 0 comments
This not really true. Put this js console and you'll see that a is the string "[object Object]":
a = {}+[]
When you put just {}+[] in your console its doing an empty block followed by unary-plus,like: {/*do nothing block*/}; +[]I guess JS’s parser explicitly prohibits adding things to a curly-brace enclosed entity?
I’m normally quite defensive of JS, but I’ll have to admit I don’t like that.
({}) + []
-> "[object Object]"
will correctly make it "[object Object]"Did you try to do this?
{a: 1} + []
-> 0
That's interpreted as the statement 1 labeled a, not as an object. This makes it obvious it's not an object: {a: 1, b: 2} + []
-> Uncaught SyntaxError: Unexpected token ':'
JavaScript supports labels so you can continue/break multiple levels at once: {
a: while (true)
while (true)
break a;
}You can also do like ({} + []) and it will actually be "adding" them; the confusion solely comes from people typing into the JS console something that wouldn't make any sense to put into an actual script.