Arrays and functions

PHOTO EMBED

Fri Oct 01 2021 12:09:11 GMT+0000 (Coordinated Universal Time)

Saved by @ianvalentino #javascript

const concept = ['arrays', 'can', 'be', 'mutated'];

function changeArr(arr){
  arr[3] = 'MUTATED';
}

changeArr(concept);

console.log(concept);

const removeElement = newArr => {
  newArr.pop()
};

removeElement(concept);

console.log(concept);
content_copyCOPY

So when you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well. You might also see this concept explained as pass-by-reference since what we’re actually passing the function is a reference to where the variable memory is stored and changing the memory.

https://www.codecademy.com/courses/introduction-to-javascript/lessons/arrays/exercises/array-in-functions