2

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
    }
Stephan Møller
  • 1,247
  • 19
  • 39

3 Answers3

6

You said they are objects so of course they are not the same:

var g = new String("game1"),
    g2 = new String("game1");

g===g2 //false
g==g2 //false

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, var is automatically converted toString form:

null + "hello" === "nullhello"
Esailija
  • 138,174
  • 23
  • 272
  • 326
3

Theoretically, if the type is object...

id1 = { toString: function() { return "game1" }, player: "monkey" };
id2 = { toString: function() { return "game1" }, player: "tiger" };

id1 === id2                        // false
"id1: " + id1 + "; id2: " + id2    // id1: game1; id2: game1
typeof(id1)                        // object

Basically, you have two things that both render to the same string, but are not the same.

Amadan
  • 191,408
  • 23
  • 240
  • 301
2

Triple equals means the type must be equal as well. So apparently, your variables id1 and id2 are not of the same type.

In your example, you are defining g and g2 to both be strings with value "game1", so the triple equals works in that case.

You need to check the type of playerTick.gameId and that.gameId. One of them is not a string.

Logan Serman
  • 29,447
  • 27
  • 102
  • 141
  • 1
    [It means no such thing.](http://stackoverflow.com/questions/7615214/in-javascript-why-is-0-equal-to-false-but-not-false-by-itself/7615342#7615342) That's not how JavaScript works. – Incognito Dec 22 '11 at 17:46
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Identity_.2F_strict_equality_(.3D.3D.3D) Seems pretty accurate to me. – Logan Serman Oct 19 '14 at 21:59