sanman
sanman

Reputation: 780

Comparing objects in JavaScript

I have this code:

var object1 = {same:'test'}
var object2 = {same:'test'};       
console.log(object1 === object2)

It returns false in the console.

I also have this code:

var object1 = {same:'test'}
var object2 = object1;
console.log(object1 === object2)  

It returns true in the console.

I know '===' is an equality operator, but I don't know how it works on objects.

Why does the first example return false?

Upvotes: 33

Views: 3112

Answers (10)

Mohammed Alawneh
Mohammed Alawneh

Reputation: 306

This is how javascript handling and comparing objects:

enter image description here

Upvotes: 0

Ahmet Can Güven
Ahmet Can Güven

Reputation: 5462

Two different object can't be equal to each other by definition in JavaScript. Equal attributes and values are not important. They have different pointers (first example).

In the second example, you are cloning the object with the reference. So "===" will return true.

Upvotes: 3

Your example does not relate to the == vs === operator difference. As people explained before, your example simply creates two different object and in the next example reference the same. Both == and === would act the same there.

Since objects litterals are always objects litterals and not in any way implicitly convertible, == and === will always act the same for objects litterals. Some other types like NaN, null and the like, you get weird/funny results when comparing with == and ===

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

See this ball? Its colour is red. Call it ball1.

See this ball? Its colour is red. Call it ball2.

Is ball1 the same object as ball2? No, they are distinct objects that happen to have identical properties.


See this ball? Its colour is red. Call it ball1.

Let's call ball1, ball2.

Is ball1 the same object as ball2? Yes. They are the same ball.

Upvotes: 83

knittl
knittl

Reputation: 265141

Objects are compared by reference equality, and since object1 and object2 are two distinct instances, they are different objects (think of it as: they occupy different places in memory).

If however you assign object1 to object2, they both point to the same place in memory and thus are equal (they are two references to the same single object, it only exists once in memory). Changing a property through object1.same = 'uiae'; will update the property object2.same as well (because they are in fact the same identical object and the same property)

Upvotes: 36

Srinu Chinka
Srinu Chinka

Reputation: 1491

var object1 ={same:'test'}
var object2 ={same:'test'};       
console.log(object1 === object2)

In this case you are creating two different objects. that's why it returns false value in console when checking with === operator.

var object1 ={same:'test'}
var object2 =object1;
console.log(object1 === object2)

In this case you are creating one object and object1 & object2 referencing to created object, that's why it returns true when you are checking with === operator.

In second case if we change the object2["same"]="test2" then object1["same"] value also be changes to test2.

In first case it won't happens like that, because object1 & object2 are two different objects.

refer this: Object Equality in JavaScript

hopes this is may be helpful.

Upvotes: 9

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

In the first case Object1 and Object2 are two different reference points(or variables) pointing at two different objects in memory.so when you check them for equality the result is false.

In the second case you are just copying the reference point(address) from Object1 to Object2 meaning both the reference points are now referring the same object in memory.so the result true.

Objects are always compared by reference.

Upvotes: 6

Waog
Waog

Reputation: 7245

In your first example you created two distinct objects with arbitrary content, which is randomly the same: {same:'test'} and {same:'test'}

In your second example you created only one object, and stored it in two variables: object1 and object2

The === checks for object identity, that means the objects in the compared variables must be the same.

Upvotes: 2

biplav
biplav

Reputation: 791

When you have created object this way, you have created a map. There is nothing like value of a map as its not of primitive types. So equals would just check for the reference equality. Hence this fails. In the second case, the reference equality goes through.

Upvotes: 3

Barmar
Barmar

Reputation: 780688

When you assign objects or arrays from one variable to another, it copies a reference to the original object, so the two objects are equal.

Each time you write an object or array literal, it makes a different object or array, so they're not equal, even if the contents are similar.

Upvotes: 4

Related Questions