Comparing Objects

In JavaScript objects are a reference type. Two distinct objects are never equal, even if they contain same properties. If the same object reference is compared, then it returns true.

For Example :

var veggies = { name : 'potato' };
var vegetable = { name : 'potato' };

Two variables and the two distinct objects with same properties :

veggies == vegetable // returns false;
veggies === vegetable // returns false;

Two variables and a single object :

var veggies = { name : 'potato' };
var vegetable = veggies ;

As the veggies and vegetable are pointing to the same object, it returns true.

veggies == vegetable // returns true;
veggies === vegetable // returns true;

If the name gets updated, then the changes are reflected in the reference object also.

veggies.name = 'carrot';
console.log(vegetable); // returns { name : 'carrot' }




No comments:

Post a Comment

Overview of AngularJS

AngularJS is an open source web application framework. It is now maintained by Google . The most of the code can be eliminated by using th...