Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Object inside an array keep the old obj reference after changing obj value

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?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

In other words, you are not deleting the object, but rather the pointer to it. Object variables are actually pointers to the object itself. So that's why you can have many pointers to same object. Each will affect it. But removing a pointer doesn't remove the object unless it's the final pointer to it.

about 4 years ago · Santiago Gelvez Report

0

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++

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!