eval(string)
Executes the code it’s passed with the privileges of the caller.
var x = 2;
var y = 39;
var z = '42';
eval('x + y + 1'); // returns 42
eval(z); // returns 42
var x = 5;
var str = "if (x == 5) {console.log('z is 42'); z = 42; x = 420; } else z = 0;";
console.log('x is ', eval(str)); // z is 42 x is 420
var fctStr1 = 'function a() {}'
var fctStr2 = '(function a() {})'
var fct1 = eval(fctStr1) // return undefined
var fct2 = eval(fctStr2) // return a function
Sources:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Comments