nahab
nahab

Reputation: 1354

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

Why next code is valid Javascript code?

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

alert(global);

Upvotes: 6

Views: 166

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263047

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);

Upvotes: 10

Related Questions