JSFuck

From Esolang
Jump to navigation Jump to search

JSFuck is an esoteric subset of the JavaScript language that uses only six distinct characters in the source code. The characters are +, !, (, ), [, ].

Description

JSFuck works because every JavaScript program can be written as a string that gets evaluated.

For example, this normal JavaScript code:

alert("Hello, world!")

can be written as

Function('alert("Hello, world!")')

Now each character of the string alert("Hello, world!") can be written using only the 6 characters +!()[] in a tricky way. For a start, let's just look at the letter "a", how it's written in JSFuck:

  • "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!

In a few steps, the letter "a" has been converted into pure JSFuck code. Other characters can be much harder to produce, but over the years, tricky ways have been found to write any character using only the characters allowed in JSFuck. Anyway, those can be quite long strings for each character, and that's why a simple Hello, World! program in JSFuck takes up more than 10000 characters!

But let's come back to our example: after all JSFucked characters are joined back together like "a"+"l"+"e"+"r"+"t"+..., there's still the Function(...)() part to convert. Function in JavaScript is the constructor of any function, so if we can get a function somehow, and then get its constructor, we're done. In fact, Function in JSFuck is made from []["flat"]["constructor"], i.e., the "constructor" property of the "flat" property of an empty array. The strings "flat" and "constructor" are just sequences of characters, and we already know that there's always a tricky way to write each character.

Encoders

There are a few encoders available for JSFuck, all written in JavaScript:

Hieroglyphy is not a real JSFuck encode, because it uses extra characters like {, }, but it's listed here for completeness: