2

So I am looking for something similar to this question python if statement with variable mathematical operator but in jQuery/Javascript

Essentially something like

var one = "4";

var two = "6";

var op = "==";

if (one op two) {
//do something
}

is this possible?

Community
  • 1
  • 1
thisuser
  • 129
  • 3
  • 10

5 Answers5

11

You could define a lot of binary functions:

var operators = {
    "==": function(a,b){return a==b;},
    "<=": function(a,b){return a<=b;},
    ">=": function(a,b){return a>=b;},
    "<": function(a,b){return a<b;},
    ">": function(a,b){return a>b;},
    …
};

var one = "4",
    two = "6",
    op = "==";
if (op in operators && operators[op](+one, +two)) {
    //do something
}

If you don't want to generate such a large object and have no complex functions, you also might be able to generate them on-the-fly (using a bit of eval magic):

var validOps = /^([!=<>]=|<|>)$/,
    operators = {};
function check(op, x, y) {
    if (arguments.length > 1)
        return check(op)(x, y);
    if (op in operators)
        return operators[op];
    if (validOps.test(op))
        return operators[op] = new Function("a","b","return a "+op+" b;");
    return function(a, b){return false;};
}

if (check("==", 4, 6)) {
    // do something
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

You can use eval() but it should be a string that could be evaluated to javascript to get the desired results.

Live Demo

if (eval(one + op + two)) {
    //do something
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • the regular warnings on eval... be careful and make sure you know what your evaluating – jdennison Oct 18 '12 at 18:19
  • Well this works, but I've never used eval before... what is so wrong with it? – thisuser Oct 18 '12 at 18:22
  • Don't use eval. At least, use `eval("one"+op+"two")` so you only need to take care of validating operands – Bergi Oct 18 '12 at 18:25
  • No need to downvote this answer.. This is perfektly correct .... but it uses eval.. @user1735913 you can eg. look here.. http://stackoverflow.com/questions/4270597/why-not-eval-json – Andreas Louv Oct 18 '12 at 18:25
  • 2
    Not my downvote, but any mention of eval generally gets you at least one. @user1735913 details on the evils of eval http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – James Montagne Oct 18 '12 at 18:26
  • There are better approaches than using `eval`. I strongly recommend avoiding `eval` at all costs. – zzzzBov Oct 18 '12 at 18:26
  • I will use the answer I selected as the answer. But if I am only using this function to choose whether or not to hide or display a div based on the equation that is generated, is it really that bad to use eval in this case? – thisuser Oct 18 '12 at 18:33
1

If you have an operation (otherwise known as a function) that you'd like to perform on two variables, then you simply need to define it:

var operations,
    a,
    b,
    op;
operations = {
    '==': function (a, b) {
        return a == b;
    },
    '&&': function (a, b) {
        return a && b;
    },
    '||': function (a, b) {
        return a || b;
    }
};
a = 4;
b = 6;
op = operations['=='];
if (op(a, b)) {
    //do stuff
}
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

I needed something like this recently and ended up writing a function to parse the operator.

Something like:

function checkLogic(one, op, two) {
    switch(op) {
        case '==':
            return one == two;
        // etc
    }
}
freejosh
  • 11,263
  • 4
  • 33
  • 47
-2

You can use Eval for this

 if(eval(one+op+two)){
//do something
}
Ali Abbas
  • 126
  • 2
  • 9