top | item 12666361

A Javascript journey with only six characters

567 points| Jazcash | 9 years ago |jazcash.com | reply

112 comments

order
[+] qwertyuiop924|9 years ago|reply
I'm a fan of Javascript. It has proper lambdas, true lexical scope, will soon have TCO, and is a really flexable language.

But it's not without its warts, and this is one of the worst. Although it's sometimes fun to mess with, nonetheless.

To see this taken to one of its logical extremes, check out If Hemmingway Wrote Javascript's entry for Douglas Adams:

  // Here I am, brain the size of a planet, and they ask me to write JavaScript...
  function kevinTheNumberMentioner(_){
    l=[]
    /* mostly harmless --> */ with(l) {
 
      // sorry about all this, my babel fish has a headache today...
      for (ll=!+[]+!![];ll<_+(+!![]);ll++) {
        lll=+!![];
        while(ll%++lll);
        // I've got this terrible pain in all the semicolons down my right hand side
        (ll==lll)&&push(ll);
      }
      forEach(alert);
    }
 
    // you're really not going to like this...
    return [!+[]+!+[]+!+[]+!+[]]+[!+[]+!+[]];
  }
[+] digi_owl|9 years ago|reply
That reads like a close cousin of brainfuck...
[+] djsumdog|9 years ago|reply
For those of you too lazy to open up a Javascript console, it's a function that returns "42".
[+] z3t4|9 years ago|reply
Lexical scope with async closures makes it very easy to write async code for example events in a gui or handling multiple clients in a server. Its a big deal.
[+] madflame991|9 years ago|reply
The title is incorrect; you need "!" and the only reason why I clicked on the article was that doing it without "!" would be a BIG deal. The title of the original article is "A Javascript journey with only six characters" and the topic has been posted/reposted and explained more times than I can count on HN
[+] Jazcash|9 years ago|reply
Oh sorry, how could I miss that :( Fixed it now
[+] tempodox|9 years ago|reply
This is an extreme demonstration of the validity of a delightfully snarky blog post by Robert Harper on how dynamic typing is actually static typing using a unitype:

https://existentialtype.wordpress.com/2011/03/19/dynamic-lan...

A string is a Boolean is a number is a function, and braindead conversions can happen without anyone noticing. How does one keep their sanity using a language like that?

[+] jorams|9 years ago|reply
What the OP takes to the extreme is a property of weak typing, not dynamic typing.

These properties are separate issues. A language is either statically typed or dynamically typed, and it is also either strongly typed or weakly typed.

In a statically typed language, types are attached to variables. In a dynamically typed language, types are attached to values. Do note that many languages don't fit 100% into either category. (Note that the blog post you linked to calls types attached to values classes, but that distinction often isn't made so clearly.)

A weakly typed language performs implicit type conversions as it deems necessary, while a strongly typed language does no implicit type conversions. Most languages don't fit 100% into either category. Usually languages that are considered to be strongly typed still allow you to add integers to floating point numbers, for example.

It is possible for a dynamically typed language to be so strongly typed that it won't do any implicit type conversion, ever. Such a language would not allow you to, say, add an integer and a floating point number without explicitly converting one to the other.

It is also possible for a statically typed language to be so weakly typed that it implicitly converts types everywhere. Such a language might do the exact same things the OP uses, like converting a function to a string of its source code when adding it to a string.

[+] qwertyuiop924|9 years ago|reply
Keep it straight in your head, and use strict comparison.

Hey, at least when JS implicitly converts your types, it actually does type conversion, rather than merely casting them, so you often get what you want (looking at you, C).

[+] gloriousduke|9 years ago|reply
With something like flowtype.org.
[+] shp0ngle|9 years ago|reply
Why do I feel like I have read this article a few years ago? I remember it, but it has 2016...
[+] colejohnson66|9 years ago|reply
Because this technique was used to bypass EBay's JS blocker a while back.
[+] JohnDotAwesome|9 years ago|reply
Because you did. I came to the comments wondering if this was a shameless ripoff of the original or a re-post from the same author.
[+] JelteF|9 years ago|reply
The article does not explain how it gets the {}, which is used to get the Object constructor string. Other than that it's very clear.
[+] kukx|9 years ago|reply
It does, see the "fill" step: «So now we have acquired the following extra characters: c,o,v,(,),{,[,],}, .».
[+] Jazcash|9 years ago|reply
You're right. It's possible to use it later on but I've removed it as it doesn't make sense being at that point in the post chronologically. Thanks!
[+] fibo|9 years ago|reply
Wow, JavaScript is also an esoteric Language.

I remember similar strange and interesting stuff in Perl, like the spaceship operator.

[+] lell|9 years ago|reply
Reminds me a bit of the quest for alphanumeric shell code.
[+] novaleaf|9 years ago|reply
Does anyone know a good sandboxing technology to execute user written javascript in a safe way? (like, on other user machines or on the server)? I have some ideas like "learn programming" that would benefit from this immensely.
[+] wyager|9 years ago|reply
The safest way would be to embed an interpreter written in JS and remove any sort of I/O or FFI capability from the interpreter.
[+] SomeHacker44|9 years ago|reply
What about Node.js's `vm` module?
[+] sjclemmy|9 years ago|reply
This is quite timely. I was looking at a library the other day which had IIFEs preceded by '+'. I wondered what the purpose was. Now I know!
[+] keyle|9 years ago|reply
I knew about these languages but I've never understood how they were made. This is a fun, fantastic article! These articles make me excited about technology. Even bad ones.
[+] pvdebbe|9 years ago|reply
I sighed with relief that the characters were not emoji.
[+] CiPHPerCoder|9 years ago|reply
> Javascript is a weird and wondeful language that lets us write some crazy code

Wondeful! A typo six words in.

[+] omnimus|9 years ago|reply
Why do you care?
[+] amelius|9 years ago|reply
I'm getting a browser security warning on the url on Android.
[+] a_c|9 years ago|reply
I don't see the point of a programming language allowing itself braining fucking its users(developer) for serious use
[+] arundelo|9 years ago|reply
If you don't like JavaScript's type coercion rules, fair enough. But if you're criticizing the fact that you can write JavaScript programs with nothing but punctuation characters, remember that no-one ever does so except as a joke, and that you can do ridiculous stuff in most languages. (Try Googling "obfuscated $YOUR_FAVORITE_LANGUAGE".)
[+] atemerev|9 years ago|reply
All those idiosyncratic moments were taken into Javascript right from Perl. Perl can do this and much more.

And Perl codebase is huge. Perl is used quite heavily at Amazon, Booking.com and Yahoo, among others.

Say what you want about Perl, but it is fun and hacker-friendly. I enjoyed it for many years.

[+] kqr|9 years ago|reply
It's optimizing for writeability rather than readability.