2

The difference between == and === is that the former one checks values only (1 == "1" will return true) whether the latter one checks the values and additionally checks types (1 === "1" will return false since number is not string).

Comparing objects means comparing object references (object variable holds internal addresses to the objects they refer to and those addresses are being compared). If two objects have totally the same keys and values, functions, etc. but they are separate objects, == will return false so === will also return false.

The question is: does it make any difference if I use == or === comparison operator concerning JavaScript objects? PS if I'm wrong anywhere, please correct me and explain (and I'll accept it as the question answer)

edit: this is NOT about javascript primitives, so comparing objects and primitives is off-topic.

ducin
  • 25,621
  • 41
  • 157
  • 256
  • 3
    possible duplicate of [Does it matter which equals operator I use in Javascript Comparisons](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – taylorc93 Jul 10 '13 at 13:20
  • 1
    @taylorc93 I ask only in case of objects, not all types (including primitives). – ducin Jul 10 '13 at 13:23
  • Also realise that === have documentation purposes too. Use === if it is important to show that it is strictly equal and not just evaluates to equal – mplungjan Jul 10 '13 at 13:23
  • The difference between both operators is more complex than that. Learn more about them, and you'll get the answer you want. – Geeky Guy Jul 10 '13 at 13:23
  • By comparing 2 objects with `===` means that the two objects are exactly the same. For example: `a = new Object()` and `b = new Object()` and `c = a`, then `c === a` will return true and `a === b` will return false since it's not the same thing. – Paul Moldovan Jul 10 '13 at 13:23
  • @tkoomzaaskz: An answer that deals with all types, deals with objects, which is what you're asking about. –  Jul 10 '13 at 13:24
  • I've almost broken down... I tried to make my question as precise as possible. I know the difference between `==` and `===`. You don't have to give examples comparing objects and primitives, not everything in JS is an object! I'm asking only about comparing objects. === checks types additionally, but "object" is a type itself, so - in case of JS objects - == and === seem the same to me. I couldn't find exact answer to my question in possible duplicates... – ducin Jul 10 '13 at 14:10
  • *"`===` checks types additionally"* That's not quite correct. They both check types. It's what they do with that information that makes them different. Yes `==` and `===` are the same when the types are the same. –  Jul 10 '13 at 14:16
  • @CrazyTrain can you tell me how does `"1" == 1` (returning true) check types? What does this comparison operator do with this information (it has number and string)? I thought that `==` just ignores it. – ducin Jul 10 '13 at 14:20
  • 1
    @tkoomzaaskz: There are two algorithms used. The *Strict Equality Comparison Algorithm* used by `===` and the *Abstract Equality Comparison Algorithm* used by `==`. They both start by testing the internal *Type* (slightly different than `typeof`) of both operands. When the types match, the next steps in both algorithms are the same. When the types *don't* match, the Strict simply returns `false`, but the Abstract begins a process of coercing the operands to primitive types and making a recursive calls to the same Algorithm. This is done until the operands have been coerced to matching types. –  Jul 10 '13 at 14:28

1 Answers1

2

Simple comparison of user-defined objects (I assume you're asking about them and not about primitives such as string and Numbers), never returns true, so there is no point using it. You can check whether two objects are of the same type by comparing their prototypes and constructors, but then again it is indifferent wheter you use == or ===. The only difference is that the comparison may return true. But this of course does not say anything about the properties of an instance, two instances with the same prototype and constructor may have different properties

function cat1 () {
    this.name = "blacky";
    this.age =  9;
}

function cat2 () {
    this.name = "blacky";
    this.age  = 9;
}


var anton = new cat1()
var john  = new cat2()
var kevin = new cat1()

console.log(anton == kevin) // false
console.log(anton == john) // false
console.log(anton === john) // false
console.log(anton === kevin) // false
console.log(anton.__proto__ == kevin.__proto__) // true
console.log(anton.constructor == kevin.constructor) // true
console.log(anton.constructor == john.constructor) // false
console.log(anton.__proto__ == john.__proto__) // false

To conclude then, the answer to your question is: no, it does not make any difference whether you use == or === for comparing objects, because comparing them always returns false. If you want to compare user-defined (not primitive) types you should compare them directly by using proto method of an object which returns the prototype of each object.

Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
  • 1
    Doesn't this use the non standard __proto__ to check if objects are of a certain type? It doesn't check the objects value. If I have 2 date objects with totally different values than d1.__proto__===d2.__proto__ will return true because they're both dates – HMR Jul 10 '13 at 14:06
  • 3
    Thanks God, there was someone who understood my question. Thank you for reading the question precisely. Just as I thought, if 2 variables refer to the same object, comparing with == and === will always return true. And if they refer to different objects in memory, both comparisons will return false. Always. This is what the question was about. `__proto__` is a different topic and I do understand them ;) by the way, `__proto__` is non-standard and we should avoid using it. – ducin Jul 10 '13 at 14:16
  • @tkoomzaaskz: FWIW, `__proto__` is set to be standardized in ECMAScript 6. –  Jul 10 '13 at 14:18