1

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
    }
4
  • Are both variables of the same type? Because that's what === is there to check Commented Dec 22, 2011 at 17:35
  • Forgot a semicolon for g2. I would've just edited and added it in; but I don't know if you copied/pasted from original code. Commented Dec 22, 2011 at 17:36
  • Are you sure they're both strings? Commented Dec 22, 2011 at 17:37
  • 2
    Try opening your browser's console when those variables exist, and see what typeof id2 and typeof id1 tells you. Commented Dec 22, 2011 at 17:38

3 Answers 3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Heh, your example is simpler than my example :D +1
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.

Comments

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.

2 Comments

It means no such thing. That's not how JavaScript works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.