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

8bitcat
  • 2,206
  • 5
  • 30
  • 59
Frank Tian
  • 787
  • 3
  • 8
  • 22
  • 1
    possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – tkone Jan 15 '14 at 19:57
  • thx tkone for the help – Frank Tian Jan 15 '14 at 20:00
  • possible duplicate of [Javascript by reference vs. by value](http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value) – Bergi Jan 15 '14 at 20:09

1 Answers1

1

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

lexasss
  • 302
  • 1
  • 2
  • 9
  • 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. – Givi Jan 15 '14 at 20:20
  • 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; – lexasss Jan 15 '14 at 20:40