Basic JavaScript - Stand in Line

Tell us what’s happening:
I don’t understand why the arr parameter is used inside the argument, it is shown as arr.push(item) but I understood it to be written as testArray.push(item).

I also don’t understand why it is also used at another code directly below it.

  **Your code so far**
function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
return arr.shift();
// Only change code above this line
}

// Setup
const testArr = [1, 2, 3, 4, 5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Stand in Line

Link to the challenge:

inside a function you should always use the parameters, if you use variables form outside the function it will make the function work only for those variables, and not be reusable

1 Like

We use the arguments when writing the code so that the code is made in a generic way. The actual arguments passed, or put into the array can be different. For example someone may call the function with an array of [2,4,6,8,100], or an array of [1,2,3,4] regardless of what type of array, or whatever they call it, the function will recognise it according to its position in the () function parentheses. The code will then execute and do whatever it has been told to do to this ‘arr’ Think of them as placeholders.

So in your code the function is saying take the arr argument, and perform some action on it: in your case .push() and .shift(). This way you don’t have to call a specific variable, but there’s a placeholder always there as part of the function.

But how does the “arr” know the data of “myArr”, I’m still a little confused because I don’t see the connection between the two.

The specific values of the function arguments are determined in the function call.

1 Like

I get it now, thanks!
:smile:

1 Like