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

218
Views
can anyone explain me why the answer is { name : 'Linda'};
let person = { name : 'Linda'};
let members = person;
person = null

console.log(members)

the output is {name: 'Linda'} but what i've learnt about reference type is that members and person have the same memory address

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

...but what i've learnt about reference type is that members and person have the same memory address...

Sort of. They both contain a reference to the same object, which is elsewhere in memory (not literally inside the variables). (Whether that's literally a memory address or whatever is an implementation detail you don't need to care about, and may differ from JavaScript engine to JavaScript engine.)

When you do person = null, all you're doing is clearing that reference out of the person variable. Doing that has no effect at all on members or on the object that it refers to.

Let's do this step by step:

let person = { name : 'Linda'};

To handle that, the JavaScript engine creates an object in memory. That object will have some kind of way to refer to it called an object reference. For our purposes, let's say it's Ref1234 (that name has no underlying meaning, I literally just picked it at random so we had a name to use):

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−−−−−−>|   (object)    |
                        +−−−−−−−−−−−−−−−+
                        | name: "Linda" |
                        +−−−−−−−−−−−−−−−+

Then the JavaScript engine stores that reference in the variable person:

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−+−−−−>|   (object)    |
                  |     +−−−−−−−−−−−−−−−+
                  |     | name: "Linda" |
                  |     +−−−−−−−−−−−−−−−+
person:  Ref1234 −+

Then we do:

let members = person;

Which just copies the reference in person into members as well:

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−+−+−−>|   (object)    |
                  | |   +−−−−−−−−−−−−−−−+
                  | |   | name: "Linda" |
                  | |   +−−−−−−−−−−−−−−−+
person:  Ref1234 −+ |
                    |
members: Ref1234 −−−+

Then we do:

person = null;

All that does is put null in person:

                        +−−−−−−−−−−−−−−−+
Ref1234−−−−−−−−−−−−−+−−>|   (object)    |
                    |   +−−−−−−−−−−−−−−−+
                    |   | name: "Linda" |
                    |   +−−−−−−−−−−−−−−−+
person:  null       |
                    |
members: Ref1234 −−−+

That doesn't change anything else at all. So

console.log(members)

shows the object.

If you then did members = null; or the members variable went out of scope with no closure retaining it, etc., and if it was the only remaining thing containing Ref1234, then the object could be garbage-collected by the JavaScript engine. But that doesn't happen when you do person = null; because something else (members) has a copy of the reference.

about 4 years ago · Juan Pablo Isaza Report

0

When you assign person to members, member will store the address of person at this moment. Then you assign person to null so that person will point to another address but not change the value of existed address. So members still keep the old address and its value

about 4 years ago · Juan Pablo Isaza 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!