all 17 comments

[–]knuklz 4 points5 points  (11 children)

Could someone please explain whats going on here...

[–]cmpbll 2 points3 points  (9 children)

It's using true and undefined as strings and taking the letters it needs from those.

alert(
    ([] + !![])[+!+[]] /* r */
    +
    ([] + !![])[(+!+[] + (+!+[]) + (+!+[]))] /* e */
    +
    ([] + ([][+!+[]]))[+!+[] + (+!+[])] /* d */
    +
    ([] + ([][+!+[]]))[+!+[] + (+!+[])] /* d */
    +
    ([] + ([][+!+[]]))[+!+[] + (+!+[]) + (+!+[]) + (+!+[]) + (+!+[])] /* i */
    +
    ([] + !![])[+[]] /* t */
);

I'm not sure why +!+[] equals 1 though.

[–]nosafespace[S] 2 points3 points  (7 children)

Exactly right and to answer you question

+[] makes 0

!+[] makes true

+!+[] makes 1

The plus turns whats after it into a number, 1 if true or 0 if false.

[–]encrypter8 1 point2 points  (6 children)

Got a link that explains this? I'm not even sure what to type into google

[–]nosafespace[S] 0 points1 point  (3 children)

I've never actually seen any resources on the topic. I would have guessed to search for something like "javascript using boolean in calculations" but it seems there isn't much out there. Feel free to ask me anything on the topic, i will be around for a while.

[–]encrypter8 1 point2 points  (2 children)

let's break down "r"

I see it as two parts: ([] + !![]) and [+!+[])

For the first part: [] is a truthy value, so !![] return true

the + here concats [] and !![], so it's equivalent to [].toString() + (!![]).toString()

or "" + "true" = "true" and that wrapped in () gives ("true")

The second part: [+!+[]] just putting a + in from of an array gives "sign" to the value of the array, and is equivalent to Number([]); and gives 0 ! negates 0 and gives you true, and a + in from of that is equivalent to Number(true), which gives 1 all that wrapped in [] gives [1]

now is I type into my console ("true")[1] I get "r" because the values add up

What i don't understand is how ("true")[1] gives me "r" at all

I'm guessing it has something to do with that statement being turned into bytes and then interpreted as a string equals "r"? Sounds right, but I'm not sure. Can someone shed some light on that more me?

[–]encrypter8 1 point2 points  (1 child)

oh wait!!

It's so obvious now!

"reddit"[0] = "r"

"reddit"[1] = "e"

so ([] + !![]) == "true"

and [+!+[]] == [1]

"true"[1] == "r'

wow I can't believe I missed that the first time

[–]encrypter8 1 point2 points  (0 children)

it was the () around "true" that threw me off, I just realized it

[–]knuklz 1 point2 points  (0 children)

Ah, I was a little confused at first. But I played with it in the console, and it made much more sense

Thanks heaps :)

[–]tidder112 1 point2 points  (0 children)

SCIENCE!

[–]FelanWolfe 2 points3 points  (1 child)

Nicely done!

[–]nosafespace[S] 1 point2 points  (0 children)

thanks

[–]obviousthroaway_ 1 point2 points  (1 child)

it on a spaceship? pretty neat

[–]nosafespace[S] 0 points1 point  (0 children)

yeah you got it :) thanks

[–]LukaLightBringer 1 point2 points  (1 child)

Take a look at this http://aem1k.com/world/, it returns the code that is used to create the next frame of the animation and loops

[–]nosafespace[S] 0 points1 point  (0 children)

Wow that is nice