Object identity in JavaScript

javascript, python

Solution

the strict equality operator (`===`) will evaluate to true if references are the same without doing any type conversion:

var a, b, c;
a = {};
b = {};
c = a;
console.log( a === b ); //false
console.log( a === c ); //true

After shooting down two posts that have made the same mistakes, I think I should point out that it's possible for `==` to equate a reference type with a value type due to type conversion:

var a, b;
a = {
    toString: function () {
        return 'foo';
    }
};
b = 'foo';
console.log( a == b ); //true
console.log( a === b ); //false

AFAIK if you can guarantee that both variables are reference types, `==` should work just fine, but that is so rarely the case that you're better off sticking with strict comparison most of the time.

Problem

How to check that two variables point to the same object? Meaning that if I mutate it—the value pointed to by both variables will change. In Python there is `is` operator, what about JavaScript?

Original source