Let's use the console to get our answer assuming we don't know what any of this means, typing
[] + ""
in the console outputs ""
Just putting (!!+[])
returns the Boolean false
. If you append the Boolean false
to ""
, you get the String false
due to type coercion.
As expected, typing (!!+[]+"")
outputs "false"
to the console.
Moving on, in JavaScript, you can think of strings like an array of characters and you can access their character using the array notation.
So, in ((!!+[]+"")[+!![]])
, you can remove the outermost brackets to make it seem simpler. Now we have (!!+[]+"")[+!![]]
in which the first part in ()
returns the String "false"
, and the next part in []
accesses a character of the String "false"
. You can now bet that +!![]
somehow returns 1 since "false"[1]
equals "a"
.
Now let's find out how +!![]
equals 1
:
[]
is an empty array which you can think of as 0
which would be true
in JavaScript (because "in JavaScript anything 'real' is true
"), so ![] is false
and !![]
is true
.
Now we are left with +true
which is just shorthand for convert true
to a number which would be 1
. Now you can see how +!![]
evaluates to 1
and you understand (hopefully) how that obfuscated piece of code works!
("false"[1])
. Now you know why the result isa
. – Vinic(!!+[]+"")
equal to"false"
and why+!![]
equal to1
– Laryssa