DEV Community

Cover image for Code anything in Javascript, with only 8 characters
Cat McGee
Cat McGee

Posted on

Code anything in Javascript, with only 8 characters

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.

Top comments (29)

Collapse
 
coleyg profile image
Cole Geerts

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

Collapse
 
catmcgeecode profile image
Cat McGee

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

Collapse
 
seanolad profile image
Sean

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

Collapse
 
jalal246 profile image
Jalal 🚀

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

[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+([][+[]]+[])[+[]]+([][+[]]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+([]+[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[+[]+!![]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+(!![]+[])[+[]]+([][+[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][+[]]+[])[+!![]])())[!+[]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]])()([][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+([]+[][(![]+[])[+[]+!![]+!![]+!![]]+([]+{})[+!![]]+(!![]+[])[+!![]]+(!![]+[])[+[]]][([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+([]+{})[+!![]]+([][+[]]+[])[+!![]]+(![]+[])[+[]+!![]+!![]+!![]]+(!![]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+[]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([]+{})[+!![]]+(!![]+[])[+!![]]]((!![]+[])[+!![]]+(!![]+[])[!+[]+!![]+!![]]+(!![]+[])[+[]]+([][+[]]+[])[+[]]+(!![]+[])[+!![]]+([][+[]]+[])[+!![]]+([]+{})[!+[]+!![]+!![]+!![]+!![]+!![]+!![]]+(![]+[])[+[]+!![]+!![]]+([]+{})[+!![]]+([]+{})[!+[]+!+[]+!+[]+!+[]+!+[]]+(+[]+{})[+!![]]+(!![]+[])[+[]]+([][+[]]+[])[!+[]+!![]+!![]+!![]+!![]]+([]+{})[+!![]]+([][+[]]+[])[+!![]])())[!+[]+!![]+!![]]+(!![]+[])[!+[]+!![]+!![]])()(([]+{})[+[]])[+[]]+HEXA_VALUE)
Enter fullscreen mode Exit fullscreen mode

is this a virus injection in js 😂

Collapse
 
catmcgeecode profile image
Cat McGee

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

Collapse
 
jalal246 profile image
Jalal 🚀

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

Thread Thread
 
drazendotlic profile image
Drazen Dotlic

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

Collapse
 
seanolad profile image
Sean

What the F**k is that!! 👿

Collapse
 
penguintheorem profile image
Attilio Urbani

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

Collapse
 
seanolad profile image
Sean • Edited

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

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I'm too hungover for this...

Collapse
 
jonrandy profile image
Jon Randy 🎖️

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

There's a converter to encode normal javascript here

Collapse
 
catmcgeecode profile image
Cat McGee

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 😅

Collapse
 
devtony101 profile image
Miguel Manjarres

Awesome post, very nice explained! 👏🏻👏🏻

Collapse
 
catmcgeecode profile image
Cat McGee

Thank you!

Collapse
 
gkhan205 profile image
Ghazi Khan

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

Collapse
 
catmcgeecode profile image
Cat McGee

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

Collapse
 
gkhan205 profile image
Ghazi Khan

yeah. but this is damn good information.

Collapse
 
chiubaca profile image
Alex Chiu

brilliant! lol

Collapse
 
scroung720 profile image
scroung720 • Edited

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.

Collapse
 
catmcgeecode profile image
Cat McGee

Yes you are correct!

Collapse
 
mrmaint profile image
AlexInAuburn • Edited

I tried the link for JSF... and converted the line: console.log("Hello");
I pasted the result in a file and executed it with node and got this...
C:\Users\MrMaint\ts>node a.js
C:\Users\MrMaint\ts\a.js:1
[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(+[![]]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+!+[]]]+(!![]+[])[!+[]+!+[]+!+[]]+(+(!+[]+!+[]+!+[]+[+!+[]]))[(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([]+[])[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[

TypeError: Cannot read property 'eundefinednstruetundefinedr' of undefined
at Object. (C:\Users\MrMaint\ts\a.js:1:69)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

What am I missing here???

Some comments may only be visible to logged-in visitors. Sign in to view all comments.