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


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';

function removeElement(newArr){
FAQ: Arrays - Arrays and Functions
FAQ: Arrays - Arrays and Functions

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()
[ 'arrays', 'can', 'be', '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.


I don’t understand why you can write arr[3] = … instead of concept[3] = …how does it know arr is referring to the concept array?


Might one conclude that this is inside a function, where concept is the argument, and arr is the formal parameter?


There is the argument.

function removeElement(arr) {
  // arr is a reference to `concept`

When concept is passed to function, only a reference is handed to the parameter, arr. Now arr refers to concept within the function. This permits changes to take place on concept, but under a different name.

Consider that since we have access to the global array object, why would we need a parameter, at all?

function changeArr(){
  concept[3] = 'MUTATED';
 > changeArr()
<- undefined
 > concept
<- ["arrays", "can", "be", "MUTATED"]

Can this be useful to other arrays? The function is dedicated to a single named object. Hardly a function at all.

Adding a parameter lets us use this function on any array, regardless of scope or context.


Essentially the arr and newArr parameters are pointers (or as the lesson notes describe, references) to the arrays that were passed in to the functions.

A pointer being a reference to a part of the computer’s memory; in this case, we are passing a pointer to concept in memory, and calling it arr and newArr in our functions. In effect we are changing concept directly in memory (not working on a copy), but as @mtf says, just by other names (the names we’ve given to the parameters of our functions).

1 Like

I’ve learned there is a difference between a reference and a pointer but don’t ask me to recall it. That’s for others to dig into. In the past I did refer to references AS pointers until I was corrected. Dash if I can remember where that post or article is, today.

It’s easy terminology to get swept up in and end up walking away misinformed, then propagating the misinformation.

Bottom line, let’s not call references, ‘pointers’, just so that word doesn’t get stewed into the mix. Let’s stick with references. Well it may be that what’s at the other end of that is a pointer, the reference is not.

In linked lists, next() would be a pointer.

The name of an object is a reference to that object. If it’s an array, the element is the pointer. It points to wherever its data is stored. I think I even got my hands slapped for calling an array a reference object which it apparently is not.

You see where I’m going with this, eh? A slip of a term can lead us down the wrong path if we are misinformed. I’m not here to inform you or anyone else, except to say, research and follow up on terminology to get the clear picture. When you do, be sure to come back here and share, please.


Yeah that’s a fair point, and I appreciate the useful clarifications.

I may have shot a little loose from the hip… pointers are a dangerous business as I recall (:grin:), but I understand that we’re not directly handling pointers in javascript, so yeah I will take a good dollop more care with that terminology :+1:

I guess the OP may have noted that when we pass primitives (primitive data types, e.g. string, number, boolean etc.) as function parameters, that changing these in our function does not affect the original variable; they are effectively passed ‘byVal’ by JavaScript, whilst our array or object are passed ‘byRef’.


Plus. Plus.

Value references leave their value in the tip jar. Data structure references leave their address.

Here’s a fiver.


Here’s my calling card.


I’d be remiss if I didn’t bring up the need to properly understand what primitive really means in the context of programming and a particular language.

Seem to remember getting my hands slapped over misuse of this term and while I’m not posting to inform, would advise do the same for one’s own benefit, and feel free to share, if you will, please anything you can put a finger on.