javascripttypescriptevalarithmetic-expressionsmathjs

Mathjs eval function behaving differently with and without string


When I am evaluating Arithmetic operation without quotes in math.eval function null is ignored and I get proper results. Where as if I put quotes to same function it throws error.

Problem is I cannot work without string, as I have some custom functions which cannot be processed without quotes.

I am using mathjs version 4.0.1.

enter image description here


Solution

  • In math.eval(100+null), the expression 100+null is evaluated before the .eval() function is called. 100+null (as a plain JavaScript expression) evaluates to 100. When you pass a string, you're asking the Mathjs expression parser to evaluate the expression, and clearly it considers '100+null' to be a syntax error; there is no null constant in the Mathjs expression syntax.