admin管理员组

文章数量:1415460

I was wondering if it would be possible to pare an int "1" and a string "!0", and make the oute 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 acplish 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" :/

I was wondering if it would be possible to pare an int "1" and a string "!0", and make the oute 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 acplish 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" :/
Share Improve this question edited Apr 7, 2014 at 18:27 Katie asked Apr 7, 2014 at 17:37 KatieKatie 48.4k19 gold badges104 silver badges129 bronze badges 3
  • 3 You'd have to evaluate the expression buried in your string with either your own parser/interpreter or by using eval(). There's no way to make the language do it implicitly. – Pointy Commented Apr 7, 2014 at 17:40
  • en.wikipedia/wiki/Code_smell There is a better way to handle this problem – Cory Danielson Commented Apr 7, 2014 at 17:46
  • @CoryDanielson :( K I'll think of another way. Thanks guys :) – Katie Commented Apr 7, 2014 at 17:54
Add a ment  | 

4 Answers 4

Reset to default 2

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/CoryDanielson/zQjyz/

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

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 mon use-cases.

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

This works:

var a = 1;
var b = "!0"; 
if(a == eval(b))
{
   console.log(true);
} else {
console.log(false);
}

Parse the string into an integer using parseInt

if(parseInt("1")!=parseInt("0"))
{
 console.log('Try this');
}

本文标签: jqueryJavascript evaluate string as a comparison operatorStack Overflow