I have this code:
...
var id1 = playerTick.gameId;
var id2 = that.gameId;
if(id1 === id2)
{}else{
throw "GameController instantiated with gameId '" + id2 + "' but tick has gameId '" + id1 + "'";
}
And when I run it, I get the message:
GameController instantiated with gameId ‘game1’ but tick has gameId ‘game1’
How can === fail when it prints correctly ‘game1’ as the value of both properties? As a test, I have made this which works:
var g = "game1";
var g2 = "game1"
alert(g === g2); // Alerts true
Does anyone have an idea of ANY theoritical explanation of how my === can fail but the error message prints the same text? The type of both values are object.
Thankyou in advance.
UPDATE:
THE PROBLEM
It turned out, as all the replies pointed out (and thankyou for that), that the types were not identical. One of the was not a string resulting in === evaluating to false.
The problem occurred after using this function to retrieve a query parameter:
function querystring(key) {
var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
var r=[], m;
while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
return r;
}
THE FIX
function querystring(key) {
var re=new RegExp('(?:\\?|&)'+key+'=(.*?)(?=&|$)','gi');
var r=[], m;
while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
return r.toString(); // Ensures that a string is returned
}
You said they are objects so of course they are not the same:
Variables that “hold objects” actually are just references to objects. Unless both the variables refer the exact same object in memory, they will not be equal no matter what the content unless you compare
g.toString() === g2.toString() //true.Note that when you do
"hello"+var,varis automatically convertedtoStringform: