Calculate string value in javascript not using eval

0 votes

Is there a JavaScript method other than eval() for calculating a formula contained in a string?

Normally, I'd take action similar to

var apa = "12/5*9+9.4*2";
console.log(eval(apa));

So, does anyone know of any eval() alternatives?

Nov 14, 2022 in Java by Nicholas
• 7,760 points
2,740 views

1 answer to this question.

0 votes

You could use the Function() constructor:

function evil(fn) {
  return new Function('return ' + fn)();
}

console.log(evil('12/5*9+9.4*2')); // => 40.4
answered Nov 15, 2022 by Damonlang
• 1,230 points

Related Questions In Java

0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,920 points
5,759 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
2,415 views
0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,480 points
3,847 views
0 votes
1 answer

How to evaluate expression given as a string in R?

The eval() function evaluates an expression, but "5+5" is a string, ...READ MORE

answered Jun 7, 2018 in Data Analytics by DataKing99
• 8,250 points
4,771 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
757 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,534 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
795 views
0 votes
1 answer

How to draw a circle in HTML5 Canvas using JavaScript?

Here's how to draw a circle in ...READ MORE

answered Nov 15, 2022 in Java by Damonlang
• 1,230 points
965 views
0 votes
1 answer

Check if a variable is a string in JavaScript

This is what I've found to be ...READ MORE

answered Nov 15, 2022 in Java by Damonlang
• 1,230 points
465 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP