1

=== checks the object equality based on references, then why setting object = null not changing the same object referenced inside an array ?

let john = {
  name: "John"
};

let array = [john];

console.log(array[0] === john); // true

john = null; // set john to null 

console.log(array[0] === john); // false -- why not true ?

console.log(array[0]); // {name: "John"} -- why not null ?
Rajesh
  • 24,354
  • 5
  • 48
  • 79
Manjiri K
  • 126
  • 8
  • 1
    GC only occurs once nothing more can reference the variable, *and* after a delay. `array[0]` still has a reference to the object. (There's no way to even determine via JS if GC has occurred - it's invisible to the implementation, other than if you get an OOM error) – CertainPerformance Oct 17 '19 at 07:30
  • 2
    This has nothing to do with garbage collection. That `array[0] === john` is the same as `array[0] === null`. `array[0]` is a reference to the same object as `john` once ___referenced___ – Cerbrus Oct 17 '19 at 07:32
  • 1
    First log returns true as you are comparing same object's reference. When you do `john = null`, you are updating pointer in `john` to some other location, so original content of `john` is still intact and is referred by `array[0]`. Hence `john` is null but not `array[0]` – Rajesh Oct 17 '19 at 07:34
  • 1
    @koolm, please be aware that `===` checks for __identity__. If you want to check equality, you use `==`. [Comparaison operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators) – Florian Oct 17 '19 at 07:35
  • 1
    After changing object john value to null, it reference is change, therefore it will give false for comparision array[0] === john – Saima Haji Oct 17 '19 at 07:35
  • I was thinking by setting object = null I am setting object to the null value, and not the pointer of that object, Thanks for all the explanations!! – Manjiri K Oct 17 '19 at 07:44
  • 1
    `// This line does three different things: creates an anonymous object in memory, create a variable named "john", and set the value of this variable to the reference to the anonymous object` `let john = { name: "John" };` – giuseppedeponte Oct 17 '19 at 07:52
  • 1
    `// This line copies the value of the variable name "john" (i.e. the reference to the anonymous object) as the first item a new array. There is no link between this array and the variable "john" once the value has been copied` `let array = [john];` – giuseppedeponte Oct 17 '19 at 07:52
  • 1
    `// The comparison is between the VALUE of the first item of the array and the VALUE of the variable "john"` `console.log(array[0] === john); // true` – giuseppedeponte Oct 17 '19 at 07:53
  • 1
    `// This line change the value of the variable named "john" to null. Nothing happens in the array.` `john = null; // set john to null` – giuseppedeponte Oct 17 '19 at 07:53
  • 2
    `// The value of the first item of the array is still the reference to the anonymous object, the value of the variable "john" is null` `console.log(array[0] === john); console.log(array[0]);` – giuseppedeponte Oct 17 '19 at 07:53
  • 2
    And since you were wondering about garbage collection, as long as the array still holds a reference to the anonymous object, this will not be garbage collected. If you set `array[0] = null`, you will delete the last reference to the object: you won't be able to access this object anymore and it probably will be garbage collected as some point. – giuseppedeponte Oct 17 '19 at 07:56
  • wow @giuseppedeponte now it's super clear.. Thanks lot.!! – Manjiri K Oct 17 '19 at 08:12

0 Answers0