In javaScript, is there a way to store a condition in a variable and then evaluate that condition later on.
I know this can be done using eval()
var condition = "(foo == pie);"
alert( eval(condition) );
The value of the alert above will change depending on the values of foo
& pie
.
Is there a similar way to do this without using eval()
?
In javaScript, is there a way to store a condition in a variable and then evaluate that condition later on. var condition = "(foo == pie);" alert( eval(condition) ); The value of the alert above will change depending on the values of foo & pie .
In JavaScript, functions are called Function Objects because they are objects. Just like objects, functions have properties and methods, they can be stored in a variable or an array, and be passed as arguments to other functions.
Should you define a variable inside IF statement? Honestly, there's no right or wrong answer to this question. JavaScript allows it, so you can make your decision from there.
This really looks like what a function is :
var conditionChecker = function(){ return foo == pie };
alert( conditionChecker() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With