How to check if two vars have the same reference? How to check if two vars have the same reference? javascript javascript

How to check if two vars have the same reference?


You use == or === :

var thesame = obj1===obj2;

From the MDN :

If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.


The equality and strict equality operators will both tell you if two variables point to the same object.

foo == barfoo === bar


For reference type like objects, == or === operators check its reference only.

e.g

let a= { text:'my text', val:'my val'}let b= { text:'my text', val:'my val'}

here a==b will be false as reference of both variables are different though their content are same.

but if I change it to

a=b

and if i check now a==b then it will be true , since reference of both variable are same now.