0
var a = 0;
var b = a;
b = 9;
console.log(a); // 0; changing b does not change a.

var c = [1, 2, 3];
var d = c;
d[0] = "hi";
console.log(c); // ["hi", 2, 3]; changing d[0] also changes c[0]!  Why?!

I thought "=" only assigns things to variables, however in the case of arrays, it appears that "=" makes both arrays directly linked to each other. Using "=" with integers (as an example) does not yield the same behavior. Why is this? This makes me rethink how I would make a separate storing location for an array. Are there any other sorts of similar behavior that may not be so obvious?

sdfsdf
  • 5,052
  • 9
  • 42
  • 75
  • 4
    Because an array is an object, so when you assign that object to something you're really storing a reference to it. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects – Jan Jul 27 '15 at 00:11
  • 2
    You should look at [deep vs. shallow copying](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy). – erip Jul 27 '15 at 00:13
  • You cannot change immutable data types, see more [here][1]. [1]: http://stackoverflow.com/questions/3200211/what-does-immutable-mean – Vidul Jul 27 '15 at 00:21
  • Lots of good info here that answers your question: [Javascript by reference vs. by value](http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value). Also [Pointer behavior by objects](http://stackoverflow.com/questions/14557654/pointer-behavior-between-objects/14557727#14557727). – jfriend00 Jul 27 '15 at 00:46

3 Answers3

3

Primitives (null, booleans, numbers, strings, etc.) are passed by value. Setting a variable stores that value in the variable. Setting another variable copies the value into that variable. They are two separate things.

Objects (arrays, functions, objects, regular expressions etc.) are passed by reference. A variable is just a pointer to where that object is stored. Setting a second variable to the first copies the reference (not value). Both variables now point to the same object in memory. Also, overwriting one of those variables means that variable no longer points to the original object… but it doesn't do anything to that original object, it just loses the reference to it.

Gabriel L.
  • 1,629
  • 1
  • 17
  • 18
  • Ok. So in short, the answer is: because an array isn't of a primitive type, and integers are. Correct? – sdfsdf Jul 27 '15 at 00:24
  • "Objects (arrays, functions, objects, regular expressions etc.) are passed by reference" --- that's not precise. Objects are not passed at all, but references to objects are passed by values. https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing – zerkms Jul 27 '15 at 00:25
  • @zerkms: Everything is passed by value, but some values are references. – Vidul Jul 27 '15 at 00:27
  • @CPH4 that's what I exactly said in "but references to objects are passed by values" – zerkms Jul 27 '15 at 00:27
0

You have two variables that both refer to the same array.
Changes to that array instance are reflected in both variables.

This situation cannot happen with numbers because numbers are immutable – it is impossible to change an existing number value.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

the variable c is a reference to an array, when you say d = c; all you have here are 2 distinct vars that point to the same array.

Your code:

var c = [1, 2, 3];
var d = c;
d[0] = "hi";
console.log(c); 

Then do something like this:

d=null;
console.log(c); // same result

All we are doing above is telling d that it now refer to null, c continues to refer to the array.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • Then couldn't you say b and a are pointing to the same integer? – sdfsdf Jul 27 '15 at 00:16
  • @MichaelL. you technically could. But then you don't reassign `d` to anything else. So 2 cases are just different. – zerkms Jul 27 '15 at 00:17
  • @MichaelL No because integers are a value type, not a reference type. – dmeglio Jul 27 '15 at 00:18
  • @TMcKeown being immutable/value type does not mean 2 variables cannot refer to the same data in memory. And specification does not state anything about how you implement that /cc dman2306 – zerkms Jul 27 '15 at 00:20
  • A number is an abstract mathematical concept. You can't write down an actual number. All you can write is a reference to a number usually in the form of a sequence of digits. If you then change those digits you haven't changed the number itself, you just changed the reference to point to a different number. So the number itself is immutable. – Tesseract Jul 27 '15 at 00:45