JSFuck (created by Martin Kleppe) is not a framework, nor is it an esolang exactly, but an esoteric coding style for JavaScript. While Perl has an entry on esolangs (with particularly choice example code), JS is similarly abusable, even if you have to dig a bit deeper to see its syntactic weirdness and eccentricities. Luckily, we have JSFuck to show us the way. Everything written in JSFuck is valid JavaScript, and works in the browser with no additional framework or plugin. It uses just six characters and resembles brainfuck in appearance.

Here is the letter “a” in JSFuck:

(![]+[])[+!![]]

To explain how this works, we first need to understand that JavaScript is an untyped language (for those who consider it weakly typed, this discussion explains it well). What this means is you don’t specify the kind of data a variable will hold when declaring it; it’s left to the interpreter to determine. When we mix types we often invoke implicit conversions. Let’s say we’re working with a boolean value (true or false). The ! operator converts such a value to its opposite: true to false and vice versa – it’s called the “not” operator (the action of using it is to negate). Take this command:

![]

We are attempting to negate an empty array, forcing JS to treat the empty array as a bool. Since it is not a “falsey value” like null or 0 or undefined, JS determines it’s true, and the negation then makes it false. What valid reason exists to negate an empty array, I have no idea, but this is what the language lets us do. 

So, let’s say we want true, we simply negate false, like so: 

!![] 

JSFuck gets a lot of mileage out of this. From true and false, we get 1 and 0 by converting them to integers with the unary + symbol. This is not the + operator for adding two things, its actual intended usage is to convert something to a number (why???). Applying it to the empty array like so: 

+[] 

gives us zero. To get the number one, we do the same thing to true: 

+!![] 

Or simply take the zero we already have, negate it, and then turn it back into a number: 

+!+[]

With zero and one, we now have a model for how to produce the other numbers. But things get more interesting when we want to produce letters. How do we write the letter “a”? JSFuck has a clever pattern for this. It will produce the word “false” (by converting boolean false to a string) and then grab the second letter. Here is how it’s explained on the esolangs.org site:

  • "a": from string "false", second character (index 1 counting from 0)…
  • "false"[1]: "false" can be made from false+[], i.e. the boolean constant false plus an empty array…
  • (false+[])[1]: we write false as ![] (negation applied to an empty array)…
  • (![]+[])[1]: 1 is a number, we can write it as +true
  • (![]+[])[+true]: since false is ![], true is !![]
  • (![]+[])[+!![]] <<< that’s it!

A JSFuck converter can be found on github. There, we can see how to get the other letters and symbols. Most use the same strategy, grabbing letters from keywords (we get the letter “n” from the string “undefined”, “y” from “Infinity”, “O” from “Object”, etc). You can find them in the jsfuck.js file. 

At JSFuck.com you can try the converter directly.

 

h/t Rich of prostheticknowledge‚Äč