In Javascript
[x]
is a list of one element containing x
. thus
[x][0]
is just a wordy and inefficient way of saying x
.
Your example
[eval][0]('alert("hello")')
is therefore just like
eval('alert("hello")')
This kind of trickery is normally found in Javascript trojan or viruses, where the author tries to disguise what is happening.
Javascript however is full of special cases and when used with eval
the code can also have the meaning of forcing global evaluation instead of local evaluation.
function defun(s) { [eval][0](s); }
function defun2(s) { eval(s); }
defun2("function square(x){return x*x}")
---> undefined
square(12)
---> ReferenceError: square is not defined
defun("function square(x){return x*x}")
---> undefined
square(12)
---> 144
See Matteo Tassinari answer link for details (note that the linked article is a bit dated, so ignore details about webkit behavior).