Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript evaluate string as a comparison operator

I was wondering if it would be possible to compare an int "1" and a string "!0", and make the outcome true.

For example:

//Comparing 0 and "0" works...
var myVariable = 0;
var checkVariable = "0";
if(myVariable == checkVariable)
{
   console.log('Works');
}

//Comparing 1 and "!0" doesn't work:
var myVariable = 1;
var checkVariable = "!0";
if(myVariable == checkVariable)
{
   //I would LIKE this to be true!
   console.log('This part doesn't work');
}

Any ideas on how to accomplish this?

I am open to suggestions, and "!=0" would also be fine as well.

////////////////////////////////////////////////// Update:

So I'm trying eval now, and here are the results:

var myVariable = 20;
var checkVariable = "20";
eval(myVariable,checkVariable);
   //Returns "20", which is ok I guess, but it would be nice if it returned "true"

var myVariable = 21;
var checkVariable = "!20";
eval(myVariable,checkVariable);
   //Returns "21", which is ok, but it would be nice if it returned "true"

var myVariable = 21;
var checkVariable = "20";
eval(myVariable,checkVariable);
   //Returns "20", which is *wrong*

Also tried this in chrome's javascript console (@Ishita):

var myVariable = 21;
var checkVariable = "!20";
myVariable == eval(checkVariable);
   //Returns "false", which should be "true" :/
like image 424
Katie Avatar asked Jun 14 '26 22:06

Katie


2 Answers

Don't use eval it's widely considered to be one of the worst parts of Javascript and isn't needed at all in this case. (see bottom of this answer for more info on that)

Something like this would be an appropriate solution:

JSFiddle: http://jsfiddle.net/CoryDanielson/zQjyz/

First, setup a map of checkVariables and functions that implement the expected comparison. All these functions do is accept a number and return true/false based on the result of a comparison.

var checkFunctions = {
    "0":  function(num) { return parseFloat(num) === 0  },
    "!0": function(num) { return parseFloat(num) !== 0; }
};

Next, modify your if statements to fetch the proper checkFunction based on the checkVariable and pass the myVariable into that function.

//Comparing 0 and "0" works...
var myVariable = 0;
var checkVariable = "0";

if ( checkFunctions[checkVariable](myVariable) )
{
   console.log(myVariable + " equals zero");
}

//Comparing 1 and "!0" doesn't work:
var myVariable = 1;
var checkVariable = "!0";

if ( checkFunctions[checkVariable](myVariable) )
{
   console.log(myVariable + " does not equal zero.");
}

Don't use eval needlessly!

eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways of which the similar Function is not susceptible.

eval() is also generally slower than the alternatives, since it has to invoke the JS interpreter, while many other constructs are optimized by modern JS engines.

There are safe (and fast!) alternatives to eval() for common use-cases.

like image 170
Cory Danielson Avatar answered Jun 16 '26 12:06

Cory Danielson


You can use "eval" function to accomplish what you want.

like image 33
Soyhan Beyazıt Avatar answered Jun 16 '26 11:06

Soyhan Beyazıt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!