Is it possible in javascript to convert a string to an operator?
I'm building a comparator for input in AngularJS.
function comparator(operatorString, value1, value2){
return value1 converted_operator(operatorString) value2;
}
var myVar1 = comparator("<", 12, 13);//true
var myVar2 = comparator(">=", 12, 13);//false
var myvar3 = comparator("!=" 12, 13);//true
And there are many possible combinations of operators. I could achieve that with an if else. But I would like to know if there is any other alternative for the same.
eval()(well or vianew Function()which is essentially the same thing).evaleval()from python. Is it the same here?evalfor this, doesn't mean you should. I'd recommend Pac0's approach.