I wanna basically execute a statement stored in a var. For example, I have this,
var statement = "alert('Hi!')";
How can I make JS execute the statement stored inside the var statement? Thanks a lot guys.
I wanna basically execute a statement stored in a var. For example, I have this,
var statement = "alert('Hi!')";
How can I make JS execute the statement stored inside the var statement? Thanks a lot guys.
This will do it, but it's not recommended
var statement = "alert('Hi!')";
eval(statement)
You could use Function constructor
var func = new Function("alert('Hi!')")
func(); // call it
You could eval but this method is comparatively safer.