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' }
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