Hacker News new | past | comments | ask | show | jobs | submit login
Why does ++[[]][+[]]+[+[]] return the string "10"? (stackoverflow.com)
32 points by downboots 37 days ago | hide | past | favorite | 29 comments



This would have made a good addition to the (in)famous WAT talk:

https://www.destroyallsoftware.com/talks/wat


The WAT talk lives in fame, not infamy.


I wouldn't mind JS as much if its type system wasn't an utter disaster.

Basically everything in that WAT talk should be an error. If your code is throwing type errors, then either it's shitty code or you're trusting non-validated input, in which case you should be able to catch the type error and return an error back to whoever sent you the data.


Another good one is (in python):

print(chr(sum(range(ord(min(str(not())))))))

> ඞ

Source: https://x.com/chordbug/status/1834642829919781369


The real trick there is that not() isn't a function call. It's not applied to the empty tuple. The empty tuple is False, so not () returns True.


amogus


Did you know we almost had a lisp style language in our browsers? But Sun's marketing budget on "Java" made something that sounded similar (JavaScript) favored more

people wouldn't have these weird questions if we had used good programming languages from the start, but marketing dollars changed this timeline forever and every new generation learns about wtf-js


This has nothing to do with a language flavor though, cause it’s coercion rules at play here, which could be done right and were (I believe) ok in Java.

Lisps in browsers of that time would suffer from the same idea, which is “let’s make relaxed/ambiguous rules for noob script kids and cool one-liners”. Which is a mistake that many people made back then (perl, php, sh). Anything that has a potential for growth must have strict typing and non-funny coercion from the beginning.

It’s this naive idea that ruined whole ecosystems.


Without making it easy for people to get started, it’s unclear what scale would have been achieved during critical days when things were growing. No one could have predicted back then what the web would become, nor the need for full app development on it (things like flash and Java applets seemed more like the way that would go).

Why would you want to cut off normal people from attempting to communicate to join a community that was linking humanity together for the first time? This strikes me as elitist. Proper programmers with years of experience shouldn’t be the only ones allowed in.

We have “use strict;” for “serious” devs.


There are levels to this. You can make it harder by forcing them to adhere to some boilerplate protocols and quirky patterns and you can make [0] behave as 0. The former is a barrier, the latter is an unnecessary toy that actually messes with beginners. This idea was wrong not because beginners shouldn’t program. It’s wrong cause it makes the road unnecessarily bumpy for everyone. If you have ([T] | T) in your api, which actually was the reason for it, iirc, just make a function toT() that returns T either way.


People getting started can handle more advanced things than most people think they can. Look at Arduino, which is many people's introduction to programming. That's C/C++.


Some days I feel the biggest failure of the web is how (script lang="whatever") does not work.

Yes I know the obvious problem with that, it would probably require changing our fundamental understanding of how a web browser works, I suspect the webbrowser would need to be at it's core an application specific vm designed around untrusted input. that is strict limits on cpu time, gated device access, etc. then the dom generator, languages and rendering engine would be implemented in this asvm.

The secondary problem, is this would probably destroy one of the webs greatest assets, everything is in plain text. yes, I know, there are all sorts of hoops you can jump through to obsfucate the text, but that is the thing, the default is shipping the source around. to do otherwise takes effort. A asvm based system would default to shipping compiled binaries around, which would suck.

But fundamentally changing the web is a boil the ocean type problem, never going to happen, we are stuck with our current web, much of it good, and much of it bad.


This would have been possible within the HotJava paradigm.


You can have a C-like language without JS's typing ridiculousness. Many exist, such as Java. Blaming JS's special needs type system on Java and "marketing dollars" is a ridiculous take.

It was not in the original JS and added later on after someone asked/convinced Eich to do it (who quickly came to regret it, but by then "people were using it", and we're stuck with it today). He's talked about this a few times in interviews and talked about it on HN.


"Almost" is overstating it a bit. It's true that the original idea Eich was proposed with was "Scheme in the browser" but Sun moved in before Java was even officially announced by name. JavaScript was intended as a "companion language" to Java pretty much from the start.

It's more realistic that we'd have ended up with VBScript dominating the browser landscape instead (Microsoft half-heartedly tried to acquire Netscape before the Sun deal and it's unlike Netscape would have lasted this long without the cooperation with Sun) and nobody wants to live in that timeline.


Was VBScript that much worse than Javascript?


Au contraire; I'm sure there would be plenty of "Why does (((()(())((())...?" questions to go around...


As I understand it was called JavaScript because it was not compiled, infact its interpreted i.e a script. And it was used to enrich the HTML interactions specifically which would mean no compile in the browser which back in the day were terrible. So Netscape and Sun came together to bring us Javascript.. It was also rushed because there was some competition with Microsoft I guess with VBScript or some derivation of that. So while JS is certainly 'quirky` I would hate to see what Microsofts version might look like today.


The reason JavaScript exists is that it was targeting hobbyists and non-programmers for whom Java would have been too much of a barrier to entry. That's why it's called that and why it was referred to as a "companion language" to Java at the time.

Remember, Java applets were supposed to be how you build Rich Internet Applications.

I recommend reading the "prehistory" chapters of this book: https://dl.acm.org/doi/pdf/10.1145/3386327


What’s Java’s got to do with it? It seems you have to dig deeper in order to get to Java-WTF (but I have attended a Java quiz so it is possible).


https://github.com/aemkei/jsfuck

Has anyone seen a transpiler for regular JS into jsfuck?

It would be like maxify-obscurify


Everyone who tries `++[[]][+[]]+[+[]]` stumbles upon this. Who hasn’t?

I expected `8`. `10` left me in disbelief before I found the accepted answer on the question.


++[++[+[]][+[]]][+[]] outputs 2 as a Number (rather than as string). How high could we go with those 3 characters ([, ] and +, no spaces) with less-than-exponential scaling compared to amount of digits of the result, as Number (as string it's easy by concatenating digits of course)?

I'm thinking it's not possible without being able to use '*'


The best part is when you’ll code it as a type in Typescript.


Without concatenation it’s likely exponential since we are comparing n to log n.


I thought this was Brainfuck and was confused, as it contains an infinite loop right at the beginning.


burn it. burn it all. flames shall devour our world of decadence and sin. we are no longer redeemable


"10" is a JavaScript error code, indicating "this is JS, not Brainfuck"





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: