Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 28, 2020 03:24 pm GMT

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")()

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.

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.


Original Link: https://dev.to/catmcgeecode/code-anything-in-javascript-with-only-8-characters-5ghp

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To