let arr = [0, 1, 2, 3];
let newArr = arr; //newArr is supposed have a value like arr right?
newArr.push(5);// making change to newArr NOT to arr
console.log(newArr); //returns [0, 1, 2, 3, 5]
console.log(arr); //returns [0, 1, 2, 3, 5]
Because arrays are objects. JavaScript keeps track of objects with the location in memory where they are stored (“by reference”). When you do let newArr = arr you are copying that memory address, not duplicating the array itself. This means that arr and newArr are both variables that each contain a copy of the memory address of the single array [0, 1, 2, 3].
You are bumping into the same issue from the other thread.
const myArr = [1, 2, 3]; // make an array and store its location in the variable myArr
const myOtherArr = myArr; // copy the value stored in myArr to myOtherArr, but this is the location of the array
myOtherArr.push(5); // push 5 onto the array at the location stored in the variable myOtherArr
console.log(myArr); // print the contents of the array at the location stored in myArr
console.log(myOtherArr); // print the contents of the array at the location stored in myOtherArr (same location)
Yeah, for an array you can copy with the spread operator.
const myArr = [1, 2, 3]; // make an array and store its location in the variable myArr
const myOtherArr = [...myArr]; // copy the values stored in myArr into a new array that has the location stored in myOtherArr
myOtherArr.push(5); // push 5 onto the array at the location stored in the variable myOtherArr
console.log(myArr); // print the contents of the array at the location stored in myArr
console.log(myOtherArr); // print the contents of the array at the location stored in myOtherArr (different location)
But note, if you have an array of objects or an array of arrays, what you will be copying is the location of these objects/arrays. This is known as a shallow copy.
Good catch. I’m thinking in six languages this morning and muddling my syntax. I’ve fixed the snippit!
Yeah, thinking of the single array having two names is a good way to put it. Which really highlights why that can be really bug prone, because you have two different ways to change the same thing, which can create confusion. Just imagine a person with two names!
copies of the values stored in these variables are passed in to the function. If the variables are primitive (like numbers and booleans), you get copies of the values. If the variables are objects or arrays, you get a copy of the memory location in which the data is stored.
Its more efficient than copying the entire object and lets you have functions that modify their inputs.
the values of 1 and 2 are copied, and for the array the memory addresses are copied instead of 3, 4, 5, for example (if 3 at the memory address 0x00, 0x00 is copied instead of 3 or both are copied?)
const myArr = [3, 4, 5];
let myVar1 = 1, myVar2 = 2;
function myFunction(myArg1, myArg2, myArg3) {
myArg1 = 11;
myArg2 = 12;
myArg3[0] = 13;
}
myFunction(myVar1, myVar2, myArr);
console.log(myVar1); // value was copied, so no change in data
console.log(myVar2); // value was copied, so no change in data
console.log(myArr); // location was copied, so change in data
You should not declare a variable as mutable if you do not plan on changing the value. It helps reduce bugs.
In this case, I knew I was not going to change the memory location of the array, so I used const. For the two integers though I wanted the possibility for them to change, so I used let.