Calculate String Value in Javascript, Not Using Eval

Calculate string value in javascript, not using eval

This exactly the place where you should be using eval, or you will have to loop through the string and generate the numbers. You will have to use isNaN method to do it.

Javascript calculator - eval() not working

In JavaScript, outside the scope of the strict mode, you can override JavaScript functions and keywords. In this case, you are overriding the eval function:

var eval = document.getElementById('eval');

Renaming that variable should cause your problems to fade away.

I also noticed that you are not 100% confident about the use of the parseInt function, you can read more here

Correct method of using eval() when returning values passed as string parameters

I really believe that you can avoid eval nearly everywhere, but if you do something which is bad - do it good!

var expression = '2 + 3 - (4 * 3) / 7';

console.log(eval(expression));

Mathematical operations? Easy!

function cos(input) {
return Math.cos(input);
}

function ln(input) {
return Math.log(input);
}

var expression = '2 + 3 - cos(4 * 3) / ln(7)';

console.log(eval(expression));

What you are doing with eval is absolutely weird and going against eval's nature.

Refer to a javascript object by string value - without using eval()

A couple of solutions come to mind. The first solution is hinted at in @CD..'s answer. The second is to restrict that string via a regex to just property names so you can safely use eval.

Traversing the window object to get the value (no eval)

function getValue(s) {
var keys = s.split("."), o = window, key, i, length, undef;

if (keys[0] === "window") {
keys.shift();
}

for (i = 0, length = keys.length; i < length; i++) {
key = keys[i];

if (!(key in o) || o[key] === null || o[key] === undef) {
throw new Error("Could not get value of " + s);
}

o = o[key];
}

return o;
}

Restricting the string to valid property names:

function getValue(s) {
var regex = /^[\w$][\w.]+$/, value;

if (regex.test(s)) {
try {
value = eval(s);
}
catch (error) {
throw new Error("Could not get value of " + s + " (" + error.message + ")");
}
}
else {
throw new Error("Could not get value of " + s);
}

return value;
}

To use:

var x = getValue(this.getAttribute("data-original-data-object"));

You want to avoid using eval because it can arbitrarily execute JavaScript that you may or may not have control of. In this particular case, you know the exact kind of string you want. In my opinion, I'd use a regular expression to make sure the string just contains property names separated by dots. Security speaking, there is no difference between these two lines of code:

var x = eval("window.foo");
var x = window.foo;

eval is not calculate if zero before the number

You can split the strings and parse the numbers, and then make them into a string again to use eval

var values = ["50.00024+40.04005+0.1", "0050.00024+040.04005+0.1"];values.forEach(function(value){    var newValue = value.split(/([\+\-\*\/])/).map(a => parseFloat(a) || a).join('');    var evaluated = eval(newValue);    console.log(value,"==", evaluated);});

Calculate data from form inputs without using eval( )

Eval is bad practice because it executes pure javascript and if you allow your user to type any js in a textfield and then execute that, you have built an invitation for crosssitescripting ;-)
It would allow your users to change the contents on your site and even send some manipulated data back to your server - if you have one.

What you are looking for is a library like mathjs that interprets a String as a mathematical formula. You can find some more info about that here
Evaluating a string as a mathematical expression in JavaScript

The other option would be to write your own formular-parser, if the libraries don't do what you need, but that would be some more work.



Related Topics



Leave a reply



Submit