Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using push or any array method modifies the original array but assigning it to something else doesn't?

I don't understand why assigning the array to a new value doesn't affect the new array. I know that "push" modifies the original array, splice also, and filter or slice doesn't, this is not my question. my question is that why assigning doesn't. i have looked through old questions answers saying it is passed by reference but again if it is passed by reference then changing it's value should affect the reference also.

const modify = (someArray) => {
    // modified my array
    someArray.push(1)
    // modified my original array
    someArray[0] = 'A'

    // didn't modify my array and I want to know why.
    someArray = ['whatever']
}
let myArray = ['a', 'b']
modify(myArray)
console.log(myArray) //  ["A", "b", 1]
like image 415
JS Lover Avatar asked Sep 07 '25 10:09

JS Lover


2 Answers

In Javascript (and Java or C#) everything is by default passed by value!

The important is to know, what the value is. This line let myArray = ['a', 'b'] creates new array in memory and put the reference to it into myArray. Now, the value of myArray is the reference to the memory where ['a', 'b'] reside.

When you pass it to modify function, the value - which is the reference - is copied from myArray to someArray. It means someArray can access the same memory segment of the same array, therefore change the values inside that array, but you cannot change the value of myArray (which is the reference to that array) from someArray.

like image 60
libik Avatar answered Sep 09 '25 23:09

libik


As I've described this to beginners many times, I've found that it's best to think of Objects in Javascript as being passed and assigned as a pointer (as in the way pointers work in C/C++). So, when you do this:

let a = [1,2,3];
let b = a;

You now have two variables that each have a pointer to the same [1,2,3] array. I find it works best to think about the array existing on its own and you now have two variables that each point at that array. When you assign b = a, it doesn't make a copy of the data, it just points b at the same data that a was pointing at.

If you modify that array with something like a.push(4) or assigning like a[0] = 9, then the one and only one array that both a and b point to has been modified. So, whether you access that array from a or from b, you will see the change because both variables point at the same physical array object.

But, if you reassign some other array to b like this:

b = [9,8,7];

You've just taken a new array and put a pointer to it in b. The other variable a still points to the same original array that is used to. It hasn't been changed in any way.

When you pass an array as an argument like you are doing in your modify() function, the function argument in the function is just like the b variable in the above example. It's just another variable that points at the same array. If you modify the array itself, then both variables will point at the same modified array. But, if you reassign the argument variable to now point at some other array, only that variable is affected.

like image 36
jfriend00 Avatar answered Sep 10 '25 01:09

jfriend00



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!