Basic JavaScript: Stand in Line Question

Alright so I had figured out you need to use push and shift in this lesson. The lesson explanation is alright but it lowkey expects you to remember those two concepts and apply it without them directly mentioning it. Alright, that sounds good because this is what I would have to do in coding regardless. But I kept running the code I have below and then [1] from the array kept staying. I had successfully used .push() but why wasn’t .shift() working? I finally clicked on the help video and compared my code. It was the same and the logic I was using was the same except i realized I had used return in the arr.push(item) part of the code. When I removed the return and ran the code, it worked. I feel like I grasped the main topic in this lesson, but why was the first return getting in the way? I had assumed that arr.push needed a return so it could show what it added to the array and that arr.shift didn’t need one because it removes and returns by itself… where did I go wrong? This question could have been shorter but I wanted to show my thought process

Your code so far

function nextInLine(arr, item) {
// Only change code below this line
return arr.push(item)
return arr.shift()

// Only change code above this line


// Setup
var 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/83.0.4103.97 Safari/537.36.

Challenge: Stand in Line

Link to the challenge:

The shift won’t work, because you put it under a return. In js, you can only have 1 return statement in a function (this includes if statements and other JS pre-defined functions). This can easily be fixed by just adding them both together like this.

function myFunction(parameter1, parameter2) {
  return parameter1.push(parameter2).shift();

You cannot add ANYTHING after a return statement.

1 Like

Along with the option of returning a value, using the return keyword signals to a function “You can stop now, even if there are more lines of code underneath”.

This means that with the first return in place, only return arr.push(item) was processed and the return arr.shift() line was being ignored.


no, it can’t. the push method returns a number.

@anonbubble whenever you have doubts on a method, you can always search for a more detailed description of it! google is your friend. it is totally normal for any developer to research how to use stuff, pretty often (multiple times per day, or per hour depending on familiarity with what they are doing)

1 Like

So does that mean that the return keyword should be used in the last part of any function right? Because if it stops reading code under it, it should be the last?

That’s the last resort for me during these lessons because I have faith that if I read and pay attention properly, the answers to my questions are all in the lessons anyway. But I know coding in real life applications would require a lot of googling and asking regardless. Have you noticed any concepts on here that require a google search if all else fails?

it’s not unusual to have to google the syntax for a certain method each time you use it (ex. slice and splice, pretty similar, different parameters, they can be confused with each other, or all the series of method to add at the beginning or end of an array or remove the first or last element… don’t remember them, but I can google them)

you could need to google because you don’t remember what a method does, returns or what its parameters are, or what method does a certain thing, or a way to solve an algorithm (I don’t know… like the smallest common multiple challenge that is further ahead, it is difficult to make it performant enough to not trigger a protection that stops it, and googling the algorithm is a thing that can be done, then you would need to translate it to JS)

1 Like

Generally yes, although there are exceptions. The most notable exception would be when you want to return a different result based on the state of some condition.

As a really simple example :

function giveTestFeedback(score) {
    if (score < 5) {
        return "You failed. Try again next time";
    else {
        return "You passed!";