iamdarrenhall iamdarrenhall - 1 year ago 142
Javascript Question

Dynamic JavaScript If Statement

In PHP I can do:

// $post = 10; $logic = >; $value = 100
$valid = eval("return ($post $logic $value) ? true : false;");

So the statement above would return false.

Can I do something similar in JavaScript? Thanks!


Answer Source

yes, there's eval in javascript as well. for most uses it's not considered very good practice to use it, but i can't imagine it is in php either.

var post = 10, logic = '>', value = 100;
var valid = eval(post + logic + value);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download