Why array value is changed only after first function?

Viewed 78

I have a function that reverses an array:

function reverseArray(array) {
  for(let i = 0; i <= Math.floor(array.length / 2); i++) {
    let old = array[array.length - 1 - i];
    array[array.length - 1 - i] = array[i];
    array[i] = old;
  }
  return array;
}

and a function that creates an local scope array and push values in reversed order:

function reverseArr(arr) {
  let output = [];
  for(let i of arr) {
    output.unshift(i);
  }
  arr = output;
  return arr;
}

Suppose there is an element:

let arrayValue = [1, 2, 3, 4, 5];

If i invoke the first function with arrayValue as argument, arrayValue is changed:

reverseArray(arrayValue); // [5, 4, 3, 2, 1]
console.log(arrayValue); // [5, 4, 3, 2, 1]

However if i invoke the second function with arrayValue:

reverseArr(arrayValue); //[5, 4, 3, 2, 1]
console.log(arrayValue); //[1, 2, 3, 4, 5]

arrayValue is not changed even if i assigned the reversed value to the argument before return:

arr = output;

can someone explain me why?

thanks in advance.

3 Answers

Basically you take a new array with

let output = [];

and later you assign the new array to the parameter arr

arr = output;

Now you have two object references, one arr of the outer scope and a new one of output.

To overcome this, you need to keep the object reference of array. For getting a new content in an existing array, you could empty the array and push the values of the new array.

arr.length = 0;
arr.push(...output);

When you do arr = output in reverseArr you are referring to a new array. In other words arrayValue in the outer context and arr refer to two different objects.

You cannot change the value of the variable the function is called with. If you have a reference to the object you can mutate it, but you cannot make the outside variable refer to another object.

Basically, your first function reverses the list in-place (i.e. operates on the list itself directly, without building a new list), while the second function reverses the list out-of-place, by building a new list which contents are the reverse of the original list.

When working in-place, the changes you do to array inside the function are directly applied to arrayValue.

The reason why arr = output does not work the way you intend is pretty much what the other answers refer to. Namely, arr and arrayValue are two different references ("pointers"). Initially, both arrayValue and arr "point to" the same array when the function is called. However, arr = output makes arr point to the newly built list output instead.

Related