Is there any way to do the following?
var value = foo;
execute("value = 'bar'");
console.log(value) // returns 'bar'
function execute(jsCodeString) {
// execute js code
}
Is there any way to do the following?
var value = foo;
execute("value = 'bar'");
console.log(value) // returns 'bar'
function execute(jsCodeString) {
// execute js code
}
Use eval()
eval("value = 'bar'");
Anyway, I suggest you to read about the pros and cons of using eval() When is JavaScript's eval() not evil?
You would want to use "eval" for this.
function execute(jsCodeString) {
eval(jsCodeString)
}