String to number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

String to number

How can i turn a string into the result of a calculation. For example:"4*6" into 24 . Note ive tried parseInt(); Number() and parseFloat();

26th Jul 2020, 7:36 PM
Goke Ekundayo
Goke Ekundayo - avatar
3 Answers
+ 8
var expression = "4*6"; Bad: eval(expression); Not bad: (new Function("return "+expression))(); Pro: https://www.sololearn.com/learn/704/?ref=app
26th Jul 2020, 8:15 PM
Kevin ★
+ 5
Thank you
26th Jul 2020, 8:43 PM
Goke Ekundayo
Goke Ekundayo - avatar
+ 4
Try eval()
26th Jul 2020, 8:13 PM
Russ
Russ - avatar