Live data from Hacker News

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

stackoverflow.com

11–20 of 80 posts

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

#11
post #9

Earlier quoted context omitted.

I am not a PL expert, but is this a strong vs. weak type problem or an implicit conversion problem? My layman’s (simplistic) model is that “weakly typed” languages allow entities to change their type at runtime, strongly typed languages do not. But what we have here is an expression where entities are being implicitly converted into intermediate values, along with operators that do different things based on the value…

[deleted]

[deleted]

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

#12
post #8

This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.

I don't see how this example justifies your position, though. Nobody will ever write "++[[]][+[]]+[+[]]" to mean "10" in anything but an obfuscated code contest.

[deleted]

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

#13
post #10

I wonder how people come up with this kind of code to begin with?

This comes from an attempt to write valid JavaScript without using any letters or numbers. It's partly for the sake of demonstrating it's possible and part for security testing reasons, to show that poorly written input validation filters can be bypassed.

True! you wont use it. I guess it'll end up being just an Interview Question.

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

#15
post #10

Earlier quoted context omitted.

This comes from an attempt to write valid JavaScript without using any letters or numbers. It's partly for the sake of demonstrating it's possible and part for security testing reasons, to show that poorly written input validation filters can be bypassed.

True! you wont use it. I guess it'll end up being just an Interview Question.

The correct interview question is, “Write a filter that blocks executable JavaScript but allows text.” And the correct answer is “Kobayashi Maru."

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

#17

This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.

I am not a PL expert, but is this a strong vs. weak type problem or an implicit conversion problem? My layman’s (simplistic) model is that “weakly typed” languages allow entities to change their type at runtime, strongly typed languages do not. But what we have here is an expression where entities are being implicitly converted into intermediate values, along with operators that do different things based on the value…

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.wikipedia.org/wiki/Weak_typing

Update to your update:

Strong typing does not subsume static typing. Ruby, for example, is dynamic and quite strongly typed. You can't bypass the type system at all, and the only real coercion is to boolean, which follows a very simple rule.

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

#19

This is the reason I prefer strongly typed languages. Allowing developers to play fast and loose with data types only leads to less maintainability down the road and makes code difficult to read.

there's a time in the place for everything. static/dynamic typing is a trade off between short-term development pace and long term maintenance. strong/weak typing is a trade off between enforcing high-level constraints and, well, i'm not exactly sure, but (void*) is used all the time in baremetal C . ... that said, i've never read an opinion that weak-typing to the extent possible in javascript is a great idea.
Post reply on HN