could someone please explain me this:
let obj = {name: 'puki'} const arr = [obj] arr[0] === obj // true (same ref address) obj = null console.log(arr) // [{name: 'puki'}]
how come the array is keeping the old obj ref?
Advertisement
Answer
You are confusing an object reference and the object itself.
In your code obj
is a reference (an address) to the actual object {name: 'puki'}
The array also stores the reference (the address) to the actual object.
When you overwrite the reference obj
with null
you’re not actually modifying the actual object {name: 'puki'}
or the target of the address, you’re just overwriting the reference (pointer) with the null pointer / value. There is no dereferencing in Javascript like it exists in C / C++