Live data from Hacker News

Why ++[[]][+[]]+[+[]] = 10 in JavaScript

stackoverflow.com

51–60 of 80 posts

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#51
post #48

Reminds me of the J programming language

Hold up, I think I just stumbled on to a fellow alum. (Surely no other university teaches that language besides mine, right?)

Which university are you talking about?

(I'm learning J and other APL derivatives right now by myself by experimenting with the J docs and pinging the amazing people at the J software forums).

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#52

Earlier quoted context omitted.

Wikipedia has a good page on this subject. In a nutshell, "strong/weak" typing is not formally defined. Usually languages are called weakly typed when they allow a lot of implicit type coercion or just don't strictly enforce types. It's not at all unique to dynamically typed languages. C is pretty weakly typed, for example. And a static language could just as easily have a quirk like this JavaScript thing. http://en.…

While the phrases don’t seem to have strict definitions, by the Wikipedia description of Strong Typing ,there is an overlap with Static Typing: The mandatory requirement, by a language definition, of compile-time checks for type constraint violations. That is, the compiler ensures that operations only occur on operand types that are valid for the operation. However, that is also the definition of static typing, leadi…

The thing is in a weakly typed language you can implement functions that do coercion, while a strongly typed language will not let you as generally there is a fixed type (or type class) to each parameter. That does not mean the standard built in or library functions have to do javascript levels of coercion though, I think there are better balances.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#55

Earlier quoted context omitted.

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() = ",,,"

You describe that as "not surprising"? Yikes.

It's surprising if you haven't used javascript before I guess.

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#57
post #40
post #37

This sort of thing really bothers me with Javascript. Using the unary + operator on an array should be an error. Hiding errors by having implicit type convertions doesn't help me fix those errors. You may say that users don't need to see to see strange error messages they don't understand. Quite right, what we need instead is to have a way for browsers to transmit uncaught exceptions in JS to the server.

One of the core principles of JavaScript is to avoid errors whenever possible. Using + on arrays bothers me less than the fact that there is no error for division by zero.

It's not an error to divide by zero per the IEEE floating point specification, and you'll find the same behavior in most modern languages:

http://en.wikipedia.org/wiki/Division_by_zero#In_computer_ar...

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#58
post #47

Earlier quoted context omitted.

You describe that as "not surprising"? Yikes.

You're surprised... why? You were expecting Python semantics? Unsurprisingly, JavaScript is its own language.

Surprisingly, its semantics and "features" are often bizarre and sweat-inducing when compared to other languages. (Surprising for such a widely-used language).

http://www.youtube.com/watch?v=hQVTIJBZook

Re: Why ++[[]][+[]]+[+[]] = 10 in JavaScript

#59

Earlier quoted context omitted.

You describe that as "not surprising"? Yikes.

It's surprising if you haven't used javascript before I guess.

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

#60
post #36

Seems like "this is why Javascript sucks!" is a common reaction here, which I find strange on a "hacker" community. It's a fun little brainteaser, not production code. Use your head a little?

It demonstrates language issues that can (and often do) lead to problems in production. So the response is completely appropriate.
Post reply on HN