0

I would like to evaluate a string as an expression in Javascript. I'm reading the string from a JSON which is dynamic. So, the expression can be anything. Here is the pseudo code I'm using

var formula = {
    "expression":"value * 9/5 + 32" //Dynamic JSON
}

var value = 26; // Dynamic value

var result = evaluateExpression(value, formula);

function evaluateExpression(value, formula) {
    return eval(formula.expression);
}

This is how I've been using eval(). Is there any other alternative to this? I've also considered using Math.js, which I think is overkill for my requirements.

  • 1
    We don't know what you requirements *are* in the first place. – Bartek Banachewicz Apr 05 '16 at 09:47
  • what is wrong with eval? it does what u need. – Rana Apr 05 '16 at 09:54
  • As I mentioned in the question, I just have to evaluate the dynamic string as expression. The sample string in the above example is "value * 9/5 + 32". This works fine. I just wanted to know if there's any alternate way to do this – Naveen santhosh Apr 05 '16 at 09:54
  • 1
    @Naveensanthosh and what's supposed to be in that "dynamic string"? What "expressions" are we talking about here? – Bartek Banachewicz Apr 05 '16 at 09:55
  • @Rana I've read that using eval() has some performance and security issues. Is this still a concern with modern browsers? http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Naveen santhosh Apr 05 '16 at 09:55
  • @BartekBanachewicz: By "dynamic string", I mean that the string can change and is not the same one I've mentioned in the aforementioned example. For instance, "value * 9/5 + 32" is an expression – Naveen santhosh Apr 05 '16 at 09:57
  • 1
    @Naveensanthosh I know what you meant by "dynamic string" and that's completely not what I asked about. I've also seen that example and understand that you consider that an expression, but I was asking for a more concrete requirements, not another example. Is `sin(5)` an expression? Is `[1,2,3].length` an expression? Are we talking about JS expressions, mathematical expression subset of it or, *what exactly*? – Bartek Banachewicz Apr 05 '16 at 09:59
  • @BartekBanachewicz Apologies for the misunderstanding. I'm talking about mathematical expressions. – Naveen santhosh Apr 05 '16 at 10:03
  • @Naveensanthosh In that case I think that Mathjs could be a solid choice. It has no external dependencies and will isolate your script from potential malicious input. At least should, I'd check that first. – Bartek Banachewicz Apr 05 '16 at 10:05

2 Answers2

0

An alternative to eval would be to create a parser and evaluator in javascript. This is rather trivial, but a bit tedious. eval is mostly fine, unless you're going to evaluate strings provided by one user in the other user's browser. If this is the case, you'll have to write a parser (or generate it with a tool like PEG.js).

georg
  • 211,518
  • 52
  • 313
  • 390
  • I'd say the difficulty of writing an intepreter varies vastly depending on the complication of the interpreted expression, but I'd never call it *trivial*. – Bartek Banachewicz Apr 05 '16 at 09:56
0

you could achieve the same using Function constructor

var formula = {
    "expression":"value * 9/5 + 32" //Dynamic JSON
}

var value = 26; // Dynamic value

var result = evaluateExpression(value, formula);
alert(result);

function evaluateExpression(value, formula) {
    return (new Function( 'value', 'return (' + formula.expression + ')' )(value));
    //return eval(formula.expression);
}
Easwar Raju
  • 253
  • 1
  • 4
  • Please add why this is better than `eval` – Bergi Apr 05 '16 at 10:30
  • @Bergi the statements inside new Function runs in a new scope where eval runs in the current scope. If you update a local variable in the expression passed in eval. it will be updated. Also, it will be easier to keep track of the variables that may be used in the expression string by seperating them as parameters in the Function constructor – Easwar Raju Apr 05 '16 at 11:21