0

I am a little bit confused about this topic for example

var person = "Kobe";
var another = person;

is variable another create an another copy or just reference to person? That being said, if I change person to something else, will another also be changed??

Compare to

var person = {name: "Kobe"};
var another = person;

Thanks for the help

3

1 Answer 1

1

Strings (also numbers and booleans) are copied on assignment in JS, object and function share references

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe i'm wrong, but as i remember strings behave a little differently person and another variable refer to same location in heap, before until they hold same value, but if you change value one of them new location in the heap is created.
I'm not quite sure about the internal JS engine process, but what is important from the practical point of view is that strings get copied on assignment, no matter what happens in memory. So, if person = "Mike", then another remains Kobe, and visaversa;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.