Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing objects in JavaScript

Tags:

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?

like image 847
sanman Avatar asked Aug 05 '14 06:08

sanman


People also ask

Can you compare two objects in JavaScript?

In JavaScript, we cannot directly compare two objects by equality operators (double equals == or triple equals ===) to see whether they are equal or not. Comparing two objects like this results in false even if they have the same data.

How do you compare objects with objects?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address. Syntax: public boolean equals(Object obj)

Can you use == to compare objects?

The == operator compares whether two object references point to the same object. For example: System. out.


3 Answers

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.

like image 137
Niet the Dark Absol Avatar answered Sep 20 '22 12:09

Niet the Dark Absol


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)

like image 26
knittl Avatar answered Sep 21 '22 12:09

knittl


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.

like image 30
Srinu Chinka Avatar answered Sep 20 '22 12:09

Srinu Chinka