Hacker News new | ask | show | jobs
by garybernhardt 5250 days ago
No, it's more subtle than that. By wrapping in parenthesis, you're making it an expression. When you just say "{}+[]" in e.g. Chrome, the first {} is parsed as a block. So what you see printed is the result of "+[]", which is 0. This is why {}+[] is not equal to {}+[] without parens. This may also be why Node gives different results; I'm not sure.

I didn't mention any of this in the talk (it would've killed the flow ;). Instead, I glossed over it and interpreted the syntax as any sane programmer would.

1 comments

I understand why it happens. I'm saying that what you're pointing out is not in the nature of JavaScript, it's in the REPL. It's just wrong; there is no other situation where that code would evaluate that way. node is behaving "properly", showing you what value you can expect if you, say, assign that expression to a variable.

I guess it's an amusing bit of sleight-of-hand, but using it to mock JavaScript seems, I don't know, tasteless. Doesn't it have enough problems without inventing more?

It's a joke. Relax.
Yes, I saw the joke.

To clarify, I thought the first part of the talk was funny. Here's a weird bit of Ruby. Hah! Here's a weird bit of JavaScript. Hah! Here's some JavaScript to make you think it's weird in a way it actually isn't! Uh, okay? Just don't see what's funny about that, I guess.