iamdarrenhall iamdarrenhall - 2 months ago 11
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!

Darren.

Answer

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);
Comments