0

Is it possible in Javascript to pass an array (from outer scope) to a function, and mutate the original array?

I've read several answers mentioning that f()'s arr argument is actually a copy, it's name just shadows the outer scope variable. Other mention arrays are always passed by reference, but are 'copied' (that is a confusing explanation).

But if that is the case, why does arr[0] = 'a'; find the arr reference in outer scope? But arr = ['a', 'b'] does not?

If arr = ['a', 'b'] was declared in the function with a blockscoped var type (let, const), that would make more sense..

let a = [0,0,1,1,1,2,2,3,3,4];

function f(arr, z) {
  arr[0] = 'a';
  arr = ['a', 'b']
}
f(a);
console.log(a);
[ 'a', 0, 1, 1, 1, 2, 2, 3, 3, 4 ]
  1. From within f() function, the line arr[0] = 'a' modifies arr in the outer scope

  2. But the reference arr =(without aletorconst`) should also refer to outer scope?

GN.
  • 8,672
  • 10
  • 61
  • 126
  • so you want `arr = ['a', 'b']` to mutate the array that was passed in? ... in one line? ... use `splice` ... `arr.splice(0, arr.length, 'a', 'b')` or `arr.splice(0, arr.length, ...['a', 'b'])` – Bravo May 10 '22 at 02:24
  • `should also refer to outer scope?` not if `arr` is a function argument – Bravo May 10 '22 at 02:26
  • No, I can mutate the passed array ok. Look at `arr[0] = 'a';`, that mutates the arg/array in it's defined scope ok. My question is, why is the function able to mutate it's args, but `arr = ['a', 'b']` does not work? Seems like, if we're able to get a handle on `arr` for mutation, why can't we do the same for reassigning? – GN. May 10 '22 at 02:29
  • so, you DO want `arr = ['a', 'b']` to mutate the array that was passed in - you just said so – Bravo May 10 '22 at 02:33

2 Answers2

1

First thing is arrays are always passed as reference in javascript.

Now coming to your question, when an array is passed to a function the argument variable of that function will hold that reference, in your case it's arr argument. If you mutate arr it will mutate original array as it is referring to original array but if you just assign some other value or reference to arr it will no longer hold reference to that original array and any changes to new reference won't have any effect on that array.

Shankar
  • 320
  • 1
  • 6
-1

JavaScript is pass by value. However, when passing an object as a parameter, the value is the reference.

An array is an object. When modifying the property of an object, the change will be reflected even outside of the function scope. Thus, modifying one item (e.g, a property of the array) produces said effect. However, replacing the object will only replace the value to which said reference points toward, thus the change will only be reflected within the scope of the function.

Spectric
  • 30,714
  • 6
  • 20
  • 43
  • Sorry, but this is the type of explanation that is so common and adds to confusion. "JS ALWAYS passes by value, objects are passed by value, but copied". What does that even mean? Doesn't that mean they are NOT passed as value? – GN. May 10 '22 at 02:34
  • @GN. Essentially, `arr` in the function holds the value of the reference to the array, so you can modify it and see the changes reflected outside. But if you change the value of `arr`, you are not modifying the original array, but just changing what the variable `arr` points to. – Unmitigated May 10 '22 at 02:38
  • Can you clarify one thing? I understand value, I understand reference. But what is the "value of the reference"? That bit is really tripping me up. @Unmitigated – GN. May 10 '22 at 02:46
  • @GN. It's a pointer towards the object - something that stores the address of the object in memory. – Spectric May 10 '22 at 02:52