Where did `arr` and `newArr` come from and what do they do or mean?


#1

Arrays and Functions

This lesson is about mutating an array. The part that I don’t understand it the parameters that are used, which are “arr” and “newArr” . Where did these come from and what do they do or mean?

In step 2 it says

" Let’s double check what happens if we mutate an array using a built-in method inside a function.

Under the console.log() statement, define another function named removeElement that takes a parameter of newArr . Inside the function body call .pop() on newArr ."

Here is the final code:

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

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

changeArr(concept);
console.log(concept);
function removeElement(newArr){
  newArr.pop();
}
removeElement(concept);
console.log(concept);

FAQ: Arrays - Arrays and Functions
FAQ: Arrays - Arrays and Functions
#2

They are arbitrarily chosen parameter names, nothing more. They will act as local variables for referencing our concept array.

.pop() will always remove the last item in the array. Just as .push() is last on, pop is last off.

function removeElement(newArr) {
  return newArr.pop()
}
console.log(removeElement(concept));
console.log(concept);
[ 'arrays', 'can', 'be', 'MUTATED' ]
MUTATED
[ 'arrays', 'can', 'be' ]

Above you’ll see how I’ve had the function return the popped value. .pop lets us assign the value we’ve taken off the array rather than just have it go into thin air. A good option to have as you will learn in your travels.