4

Can anyone help me with this JavaScript expression?

+[[+!![]]+[+![]]+[+!![]]+[-~!![]+-~!![]-~!![]]+[-~!![]]+[+![]]+[+!![]]+[-~!![]+-~!![]]]

A friend sent it to me and asked me to copy and paste it in the browser console.

This is the result:

10162014

If anyone could explain this to me or at least point me to right references please. Thanks!

HCN
  • 111
  • 1
  • 9
  • 2
    This is neither new or novel http://blog.avast.com/2013/02/14/malware-dollar-equals-tilde-square-brackets/ http://hackaday.com/2012/08/13/writing-javascript-without-using-any-letters-or-numbers/ – user229044 Aug 27 '14 at 04:27
  • To start with, remember that `true` is equivalent to `1` and `false` is equivalent to `0`. Also that an empty list (like `[]`) when converted to a boolean value if `false`. – Some programmer dude Aug 27 '14 at 04:28
  • 1
    I'd have a look at some of these question: http://stackoverflow.com/search?q=%5Bjavascript%5D+%22%5B%5D%22 – Felix Kling Aug 27 '14 at 04:32
  • 1
    Refer [cheat sheets](http://sla.ckers.org/forum/read.php?24,33349,33405) – Linga Aug 27 '14 at 04:33

3 Answers3

3

First break out your code to this: !![] which returns true (!! is to convert to boolean) and now + converts to number so +!![] returns 1.

![] converts to false so +![] returns 0.

~[] returns -1 and ~![] also returns -1.

~!![] returns -2.

Now, -~!![] returns 2 and -~![] returns 1.

So, combining them all returns 10162014.

All about you to know is ~, !, +, & -

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
2

![] = false; # as an expression, it's false due to the ! operatory [] = true; # as an expression, it's defined, so it's true

+!![] = 1; because +true = 1; +![] = 1; because +true = 0, because using a + operator in JS converts the boolean to an integer ref

So what he's done is basically constructed a numerical value using boolean to integer conversion, and some grouping.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
rurouni88
  • 1,165
  • 5
  • 10
1

[+!![]]+[+![]]+[+!![]]: [] is an empty array, which is truthy. ![] is thus false, !![] is true. +true forces it to a number, as 1. Similarly for +![] as 0 via false.

[-~!![]+-~!![]-~!![]]: ~ is a two's complement operator; ~1 is -2. Thus, this evaluates as -(-2)+-(-2)+-(-2), which is 6.

The remaining addends are analogous.

array + array will convert arrays to strings; thus [1]+[0]+[1]+[6]... will give the string "1016..."

The plus at start will convert it to a number.

Amadan
  • 191,408
  • 23
  • 240
  • 301