loading...
Cover image for Code anything in Javascript, with only 8 characters

Code anything in Javascript, with only 8 characters

catmcgeecode profile image Cat McGee ・4 min read

You can convert any Javascript code into these characters:

()[]{}!+

and only these characters.

Here's how:

Numbers

We can get 0 by casting an empty array using +[]. The + is the unary plus, which converts the right side into a numeric value, i.e. 0, Go ahead and type +[] into your console and you'll see 0. Then, to get 1, we can use +!![]!, because the boolean value true can be expressed as !![] and by using our unary plus again, we convert it into 1 - +!![]

Still with me?

By using our true value and summing it multiple times, we can get other numbers. In Javascript, true + true = 2, so 2 can be written as !![]+!![]. We don't need to prepend our first !![] with + this time, because the + in the middle tells Javascript we are adding numerical values.

We can keep adding these over and over again to get bigger numbers like 12, but can you think of a better way to do this? Bring on strings.

It's possible to cast strings to numbers in Javascript - for example, again using our unary plus, we can get the number 12 like this: +("1"+2). We can also do this by concatenating 1 and 2 with an empty array, like this: []+1+2. We can use this to our advantage here and, instead of summing true 12 times, we can sum it once for 1, twice for 2, cast them as strings, join the strings together, and convert it into a number.

Phew.

Let's take that step by step:

Sum true once for 1: +!![]

Sum true twice for 2: !![]+!![]

Cast 1 as a string: +!![]+[]

We don't have to cast 2 as a string, because Javascript already knows from us casting 1 as a string that we are adding strings, not numbers.

Join the strings together: (+!![]+[])+(+!![]+!![])

Convert it into a number: +((+!![]+[])+(+!![]+!![]))

And there you have it. We can write the number 12 as +((+!![]+[])+(+!![]+!![])).

Strings

Now that we have numbers, let's see where we can get other characters from.

  • true : we already know this can be obtained from !![]
  • false : the opposite of true, ![]
  • undefined:  appending a non-existing element to an array: [][[]]
  • NaN: trying to cast an object to a number: +{}
  • "[object Object]" : a javascript developer's favorite, object Object comes from adding an array and an object: []+{}

Casting them to string and accessing those like arrays will give us single characters, from which we can even get more strings! So now, along with any number we could dream of, we have

a b c d e f i j l n o r s t u

We can use the 2 here to cast a string such as 1e1000 into a number, which would give us Infinity. Even more letters!

a b c d e f i j l n o r s t u y

Let's not stop there. We can get even more characters if we exploit DOM elements.

Getting characters from the DOM

If we assume that we are using Javascript in the browser, we can also assume that we have access to the DOM elements, and get the remaining characters from there.

Something that we can do here is window.unescape which would give us every ASCII character by calling window.unescape("%" + HEXA_VALUE).

We already have u, n, e, s, c, a, and e and are only missing the p. Where would you get a p on website? From http of course! If we assume that we're in a webpage served over HTTP (or HTTPS) we can cast window.location to a string. The third character here would be p.

The only problem here is that we do not have access to window yet. But because Javascript is so wonderful (lol) we can get window like this:

Function("return location")()

However, whilst we have f we do not have F. To get an uppercase F, we can use our existing characters to form array["sort"]["constructor"] which will give us the word Function!

So now we have our window object, and we can get our p from there. Now we can use window.unescape but we don't yet have the % that we need to get every ASCII character.

Luckily, escaping "[" , like escape("[") yields the string %5B, and from that, we can obtain that percentage sign.

Now, we can get any ASCII character like this!

[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][+[]]+[])[+[]]+([][+[]]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+([]+[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[+[]+!![]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+(!![]+[])[+[]]+([][+[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][+[]]+[])[+!![]])())[!+[]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]])()([][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+([]+[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[+[]+!![]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+(!![]+[])[+[]]+([][+[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][+[]]+[])[+!![]])())[!+[]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+HEXA_VALUE)

Phew.

Then, to execute this code, we just need to use Function.

Some geniuses have discovered a (way more complicated) way to convert Javascript into only 6 characters AND without the need for the DOM. You can play with that here if you want to hurt your head a bit.

And that's it. You can successfully code in Javascript by using only 8 characters. If you didn't already think Javascript was weird, I'm sure you do now.

In your next interview, when they ask you to solve FizzBuzz, you should try it this way. See what happens.

Posted on by:

catmcgeecode profile

Cat McGee

@catmcgeecode

I like Javascript and Python and make tutorials so you can like them too.

Discussion

markdown guide
 

Reminds me of Brainfuck for sure, I love these sort of little esoteric-toy-language thingies!

 

Yeah, the 6-character version is actually called JSFuck!

 

You crack me up, I can't believe you said it reminds you of Brainf**k. You got jokes. 😂 😂

 

I've discovered that actually is a language ! Anyway nice article

 

F**k JS... I mean JSF**k. 🤪

 

i mean, i sometimes have difficulties reading code but this line:

[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][+[]]+[])[+[]]+([][+[]]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+([]+[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[+[]+!![]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+(!![]+[])[+[]]+([][+[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][+[]]+[])[+!![]])())[!+[]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]])()([][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+([]+[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[+[]+!![]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+(!![]+[])[+[]]+([][+[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][+[]]+[])[+!![]])())[!+[]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+HEXA_VALUE)

is this a virus injection in js 😂

 

You forgot a closing ) after char 364.
Just sayin' 🤪

 

Hahaha, looks like it! 😂 It's so fun

 

yeah, imagine you have to debug entire function written like this. good luck with that 😂😌

No problem! I know, I know, we'll use sourcemaps 😜😂🤣

 

What the F**k is that!! 👿

 
 

Super cool article there you have my unicorn vote. Correct me if I am wrong but where it says 'The third character here would be p.' I believe you meant 'The fourth character...' because the fourth characters on http/https is p.

 

Awesome post, very nice explained! 👏🏻👏🏻

 
 

I believe you only need 6 characters - ()+[]!

There's a converter to encode normal javascript here

 

Yeah! I mentioned this at the end of the article. It's very complicated and I don't know how it works, so I went for the less complex 8 character one 😅

 

Just great information you have shared. Thanks a lot :). After reading this post. I feel like I know nothing about JS. :(.

 

Nooo don't think that way! This post is just crazy haha

 

yeah. but this is damn good information.

 

That's like the anti minifier . compiling normal js to this notation actually makes the codes bigger. Very nice !

 

Hogwarts will be sending you a letter shortly because this is some of that Tom Riddle magic

 
 

Would this be at all useful for obfuscation?