JS eval


eval() function evaluates the parameter string as javascript codes. If the string is javascript code, return the result, else return undefined.

var x=5;
var y=eval('x+2');
alert(y); //7
alert('z'); //z


A common example, get the current value of a variable.

var x=5;
function incr(x)
{
x = x + 10;
alert(eval('x'));
}
incr(x); //15


In above example, if we want to evaluate x value globally:

var x=5;
function incr(x)
{
x = x + 10;
alert(window.eval('x'));
}
incr(x); //5