Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this code is valid: "(1,eval)('this')"

Tags:

javascript

Why next code is valid Javascript code?

var global = (1,eval)('this');

alert(global);
like image 500
nahab Avatar asked Mar 08 '12 19:03

nahab


1 Answers

That's because the comma operator returns its second operand (and evaluates both).

The code in your question is equivalent to:

1;
var global = eval('this');
alert(global);
like image 171
Frédéric Hamidi Avatar answered Oct 29 '22 09:10

Frédéric Hamidi