Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by raganwald on Wednesday December 28, 2011 @04:22PM
(#3401447)
From wtfjs.com:
[ Parent
| on HN ]
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by blurbytree on Wednesday December 28, 2011 @04:57PM
(#3401574)
Clever, but not as surprising...
(null,'cool',false,NaN,4) evaluates to the last expression - 4
Array(4) creates an array with 4 elements.
Array(4).toString() = ",,,"
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by bretthoerner on Wednesday December 28, 2011 @08:46PM
(#3402216)
You describe that as "not surprising"? Yikes.
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by Cushman on Wednesday December 28, 2011 @10:39PM
(#3402405)
You're surprised... why? You were expecting Python semantics?
Unsurprisingly, JavaScript is its own language.
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by nlz1 on Thursday December 29, 2011 @07:24AM
(#3403439)
Surprisingly, its semantics and "features" are often bizarre and sweat-inducing when compared to other languages. (Surprising for such a widely-used language).
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by blurbytree on Thursday December 29, 2011 @05:12AM
(#3403160)
It's surprising if you haven't used javascript before I guess.
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by bretthoerner on Thursday December 29, 2011 @10:29AM
(#3403909)
"Surprising" doesn't mean non-deterministic. So you're saying anyone who has simply used JavaScript shouldn't be surprised by that snippet you posted? It's a glowing example of surprising semantics.
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by pawn on Thursday December 29, 2011 @08:53AM
(#3403657)
You can use javascript for years and never come across a situation where you'd do or see several of the things found in that small statement.
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by kls on Thursday December 29, 2011 @12:46PM
(#3404458)
I made that point in another thread as well, if you stick to the meat and gravy of JavaScript you just don't hit on these nuances of the languadge. I am generally not a fan of clever programming tricks or "magic code" and if you are then JavaScript becomes a tempest, but if you are using it to glue interfaces together, build some widgets and manage events you just don't get into the ugly side of JavaScript, it's not my favorite languadge but it get's the job done and it is efficient as far as development effort goes.
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by blurbytree on Thursday December 29, 2011 @01:37PM
(#3404718)
There are only 3 things, which aren't surprising to anyone who has done much js.
1). a list of expressions eg (foo,bar,baz) evaluates to the last one.
2). Array(4) creates an empty array of 4 elements
2). Array(4).toString() = ",,,"
Re:Why ++[[]][+[]]+[+[]] = 10 in JavaScript
by bonobo on Thursday December 29, 2011 @02:49PM
(#3405051)
I don't think the comma operator wouldn't be surprising to an experienced js programmer. I can't remember a single piece of serious code where I could see it in use.
The same applies to the void operator, I can think of one or two cases at most where it would be useful, but I don't see anyone using it.