+ 3

Why( var a = 7;, var b = 7; )a===b is false & (var c = a; )a===c is true. Having problem to understand JavaScript reference type

var a = { a : 7}; var b = { a : 7}; a==b // false a===b //false While var c = a; a==c //true a===c //true

3rd Jul 2020, 5:31 AM
Gone
9 Respuestas
+ 5
despite the same content. a & b is 2 different object with different reference. so == and === here is not comparing the content of the object, but the object itself. thats why its false meanwhile c=a. make c referencing the object of a. thus both have same reference. try, c.a = 200; you'll the value of a.a will also change, because c and a using same reference
3rd Jul 2020, 6:12 AM
Taste
Taste - avatar
3rd Jul 2020, 10:17 AM
Sketch
Sketch - avatar
+ 4
Hi
3rd Jul 2020, 10:11 AM
Sketch
Sketch - avatar
+ 4
== & === checks same objects not identical objects
3rd Jul 2020, 10:12 AM
Sketch
Sketch - avatar
+ 4
Here a ≠ b as they are 2 different objects while assigning a = c makes them same object. That is why it is true.
3rd Jul 2020, 10:14 AM
Sketch
Sketch - avatar
+ 3
Hi
3rd Jul 2020, 5:36 AM
Gone
+ 3
When dealing with objects, variables assignments are considered only a reference to the object (more like an arrow referring to a building) so .. build a building (an object) assign it to a variable pointing twords it .. in real life you can't say two different buildings are equal to each other, i.e: they aren't the same building.. because they're two different ones, right? This is why it's always false if you compare two diff objects in js with == , wvn if having the same properties within But in case of assignments, eg: a = {a: 3}; C = a; You simply making different pointers (arrows) to the same building.. so when you a == c.. it gives true .. because it's the same object
3rd Jul 2020, 10:26 AM
Ali Kh
Ali Kh - avatar
+ 2
Thanks
4th Jul 2020, 12:54 AM
Gone
+ 2
Thanks to all
4th Jul 2020, 12:54 AM
Gone