Here is the letter “a” in JSFuck:
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"can be made from
false+, i.e. the boolean constant false plus an empty array…
(false+): we write false as
!(negation applied to an empty array)…
(!+): 1 is a number, we can write it as
(!+)[+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