-1

I want to use = to initiate an array in a function. However, = cannot change the array, but push can.

I want it equals to ["a", "b"]. But now the result is ["1", "2"].

I tried arr = ['a', 'b']; and also arr = ['a', 'b'].slice();. But neither works.

How can I let = work in this case?

var array = [];

init(array);
console.log(array);

function init(arr) {
  arr.push('1');
  arr.push('2');
  arr = ['a', 'b'];
}

https://jsbin.com/kiveyu/4/edit?js,console

Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
  • Perfect dupe target @torazaburo. – T.J. Crowder Jun 19 '16 at 05:18
  • JavaScript is pass by value. The local variable `array` has a reference to the same object as the global variable `array`. Because objects are *mutable*, you can make changes to the value itself. But since there is now reference between the global and local `array` variable, assigning to the local one doesn't affect the global one. – Felix Kling Jun 19 '16 at 05:18
  • Just for next time you have a question like this: **Please** use different names for different things. Calling both the outer variable `array` and also calling the `init` function's argument `array` is unnecessarily confusing. – T.J. Crowder Jun 19 '16 at 05:20
  • @T.J.Crowder yeah!! I just noticed that :P Done editing. – Hongbo Miao Jun 19 '16 at 05:27

2 Answers2

3

So the reason this happens is because you are assigning the local variable the new array, whereas prior to the assignment, the local variable held the value of the passed in array.

The parameter holds a reference to the value passed in. However, the parameter is still a local variable. Writing to that variable will only modify the local variable, and will lose the reference held.

To expand, from being called:

init(array);//call function init with value array

Next the context environment is created upon instantiation, it holds a local variable array, and the value of that variable is the same as the value of the passed in array (in your example they have the same name)

function init(array) {

After this, two values are pushed to the value of array, which is the value of the passed in array.

array.push('1');
array.push('2');

Here is where it seemed the confusion took place. The local variable array (still holding the value of the passed in array) has its value changed to a new array. The result is that the local variable array no longer holds the value of the passed in array, but now holds the value of ['a','b'].

array = ['a', 'b'];

That is why it looks like you cannot change the array by assignment - because the only thing you have access to in that scope is the local variable with regards to the original array.

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • 1
    aka what travis is saying is you need to assign array to init call.. `array = init(array)` and in the init function return the modified array. `return array` – John Ruddell Jun 19 '16 at 05:17
  • This is the correct answer. It easier to notice if the names were different, like if the function were defined as `function init(arg_array) { ... }` – Kyle Falconer Jun 19 '16 at 05:17
-2
function init(array) {
  array.push('a');
  array.push('b');
}
progysm
  • 1,072
  • 6
  • 7