Reputation: 13
if ("tester" == findrange[117])
{
var indict = 34;
}
if (keeperval[39] == "tester")
{
var indict2 = 45;
}
if (keeperval[39] == findrange[117])
{
var indict3 = 56;
}
This code will return when debugging will return these values:
indict = 34
indict2 = 45
indict3 = undefined
Does this make any sense? I just can't wrap my head around why this possibly wouldn't work!
Upvotes: 0
Views: 49
Reputation: 105885
You probably wrapped your strings in findrange[117]
and keeperval[39]
in String
, instead of simply using a literal.
Therefor, both aren't strings, but instead string objects. When you use them in a comparison with strings, it will use the object's .toString()
, therefor == "tester"
will work.
However, when both sides of an equality comparison are objects, the result will be true if and only if both objects are actually the same:
var s1 = new String("test");
var s2 = new String("test");
console.log(s1 == s2); // logs false
So instead of
findrange[117] = new String("tester");
keeperval[39] = new String("tester");
use
findrange[117] = "tester";
keeperval[39] = "tester";
Even better, exchange your equality tests with type-safe equality tests:
if ("tester" === findrange[117])
{
var indict = 34;
}
if (keeperval[39] === "tester")
{
var indict2 = 45;
}
if (keeperval[39] === findrange[117])
{
var indict3 = 56;
}
Then you will see instantly that there's something off. (fiddle)
For further reference, see MDN's JavaScript guide, especially sameness in JavaScript.
EDIT: If you're not able to change the type of the values in your arrays, use the .toString()
method before comparing:
if ("tester" === findrange[117].toString())
{
var indict = 34;
}
if (keeperval[39].toString() === "tester")
{
var indict2 = 45;
}
if (keeperval[39].toString() === findrange[117].toString())
{
var indict3 = 56;
}
Upvotes: 3
Reputation: 3596
First check the type console.log(typeof(findrange[117]));
In your case this should be an object
In JS,
compare obj to string; will compare obj.value to string value
compare obj to obj; will compare references to the object and not value
Upvotes: 0
Reputation: 7723
try groovy console :
def findrange = ["tester","nontester"]
def keeperval = ["tester"]
if ("tester" == findrange[0])
{
def indict = 34;
println indict
}
if (keeperval[0] == "tester")
{
def indict2 = 45;
println indict2
}
if (keeperval[0] == findrange[0])
{
def indict3 = 56;
println indict3
}
output :
34
45
56
Upvotes: 0
Reputation: 3407
use
keeperval[39] === findrange[117]
to understand if both of them are objects or strings.
Upvotes: 0