Object Equality

Object equality is done by reference, not value. For two objects to be equal they have to be exactly the same object. Though, when type casting or type coercion comes into play objects get some interesting behavior.

var a = { foo: 'bar' } var b = { foo: 'bar' } console.log(a === b) // false

Despite having the same properties with the same values, a and b are not equal.

var a = { foo: 'bar' , toString: function () { return this.foo } } console.log(a == 'bar') // true

If you provide a toString function it will get used when the object is coerced into a string. Unless you also provide a valueOf function...

var a = { foo: 'bar' , toString: function () { return this.foo } , valueOf: function () { return 10 } } console.log(a == 'bar') // false??? console.log(a > 9) // true console.log(a < 11) // true console.log(''+a) // 10, Ohhh

The valueOf function becomes how you get the actual value out of the object. It works well for a lot of use cases, but in most situations you don't want to be doing type coercive operations on an object, hoping that they have implemented toString/valueOf in sensible ways.