Skip to content
Advertisement

multiply string in js which is both multiplier and multiplication operator

I want to transform the example string "0.45*100" in a value (45).

Another example, if I have two variables totalCost and contractVolume, and I have the string "totalCost * 1000 / contractVolume": how can I transform the string in the resulting value? ex.

var totalCost = 10
var contractVolume = 100
var stringToCompute = "totalCost * 1000 / contractVolume"
console.log(compute(stringToCompute)) // -> 100 

Obs: I see a similar question, but in php, in the example he uses the method eval()

Url

Advertisement

Answer

you can also use eval() function in javascript

like —

eval("x + 17")
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement