Javascript - Weird issue with === and strings
<p>I have this code:</p>

<pre><code>...
var id1 = playerTick.gameId;
var id2 = that.gameId;
if(id1 === id2)
 {}else{
throw "GameController instantiated with gameId '" + id2 + "' but tick has gameId '" + id1 + "'";
}
</code></pre>

<p>And when I run it, I get the message:</p>

<p>GameController instantiated with gameId 'game1' but tick has gameId 'game1'</p>

<p>How can === fail when it prints correctly 'game1' as the value of both properties? As a test, I have made this which works:</p>

<pre><code>var g = "game1";
var g2 = "game1"
alert(g === g2); // Alerts true
</code></pre>

<p>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.</p>

<p>Thankyou in advance.</p>

<p><strong>UPDATE:</strong></p>

<p>THE PROBLEM</p>

<p>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.</p>

<p>The problem occurred after using this function to retrieve a query parameter:</p>

<pre><code>function querystring(key) {
       var re=new RegExp('(?:\\?|&amp;)'+key+'=(.*?)(?=&amp;|$)','gi');
       var r=[], m;
       while ((m=re.exec(document.location.search)) != null) r.push(m[1]);
       return r;
}
</code></pre>

<p>THE FIX</p>

<pre><code>function querystring(key) {
           var re=new RegExp('(?:\\?|&amp;)'+key+'=(.*?)(?=&amp;|$)','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
    }
</code></pre>
