How to execute an arithmetic operation inside a string in Javascript? -
i have little example:
var myoperation = "2+2"; var myoperation2="2*5/3"; var myoperation3="3+(4/2)"
is there anyway execute , result?
thanks lot in advance!
use eval
mdn: https://developer.mozilla.org/en-us/docs/javascript/reference/global_objects/eval
some people evil, designed exact purpose;
console.log(eval(myoperation)); // 4 console.log(eval(myoperation2)); // 3.33 console.log(eval(myoperation3)); // 5
Comments
Post a Comment