re: How to turn a string into an evaluated expression in JavaScript? VIEW POST


I think eval() is what you are looking for. developer.mozilla.org/en-US/docs/W... But this be a security risk since it works for executing any string as JavaScript code, so you will want to make sure that user-inputted strings are validated as a mathematical expression before using eval() on them. There may also be another way to achieve the same end result without eval(), but I'm not sure about how the program will operate.


If eval doesn't satisfy your needs, you might want to check the library MathJS, though I'm not sure how much safer their Math.evalutate is and it's probably overkill.

code of conduct - report abuse