FAQ: Arrays - The .pop() Method

This community-built FAQ covers the “The .pop() Method” exercise from the lesson “Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise The .pop() Method

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why does the example for using the .pop() method (page 8 of 12) include code that is not used in lesson?

“const removed = newItemTracker.pop();” is used in the example, with the reasoning that this variable can be used later. However, if this code is used in the instruction, the answer is marked as wrong and we are told to only use the name of the function followed by .pop(). “Gotcha” inconsistencies between the lesson and the instructions cause needless delays in learning, and confusion.

The instructions are different from the example for good reason… To see if you gleaned the right information from the lesson.

We are given an array of strings with the name chores.

We see from the example that the last item in the array will be removed if we write,

Array.pop()

except with the array name in place of Array.

What’s more, we can preserve that value just by assigning it to another variable.

temp = Array.pop()

If we do not assign the value, it will be gone. That’s what should happen in the exercise. Just pop() and then log chores to see that the last value is indeed gone.

I don’t see any inconsistency with the this lesson, the example or the instructions. Follow them to the letter.

chores.pop()
console.log(chores)
1 Like

Thank you for the response; Again, the lesson indicated a variable could be saved by using the const “removed”.

Not to contradict what the lesson is suggesting, we should consider whether we plan to re-use a variable or assign different values during our session. If so, then use let, not const.

Consider a loop where we are manipulating an array using pop() but we want to examine and use the values as they come off the list.

let list = "JavaScript".split('');
revList = [];
let temp;
while (list.length > 0) {
  temp = list.pop();
  //console.log(temp);
  if (temp === temp.toUpperCase()) {
    temp = temp.toLowerCase();
  } else {
    temp = temp.toUpperCase();
  }
  revList.push(temp);
}
console.log(revList)
["T", "P", "I", "R", "C", "s", "A", "V", "A", "j"]

Contrast with simply reversing a list…

let list = "JavaScript".split('');
revList = [];
while (list.length > 0) {
  revList.push(list.pop());    // value still preserved in new array
}
console.log(revList)
["t", "p", "i", "r", "c", "S", "a", "v", "a", "J"]
1 Like

Hello,

I’m following the JS course and my brain has difficulties understanding this .pop() method. I mean I get it but the example & how it works kinda bugs me.

const newItemTracker = ['item 0', 'item 1', 'item 2'];

const removed = newItemTracker.pop();

console.log(newItemTracker); 
// Output: [ 'item 0', 'item 1' ]
console.log(removed);
// Output: item 2

I have a hard time understanding why the last item gets removed since we only “saved it” in a variable. Obviusly saving it in a variable still applies the method.
I thought in order to remove the last item we would have to write newItemTracker.pop(); by itself on a new line.

The example says " * In the example above, calling .pop() on the newItemTracker array removed item 2 from the end."

What is “calling” ? To me I stored it in a variable I never “called” it yet…

maybe I’m overthinking but as soon as we see arrayName.pop(); no matter if it’s on it’s own or in a variable the last item is removed ?

Hope you understand what I mean…

Thx

2 Likes

The .pop() method removes the last item in the array and lets us capture it with a variable for further use, rather than just deleting it.

The method can be written as a standalone statement. We have the option of keeping the value in play with a variable. Either way, the last item is removed from the array.

When we call a function or method, we are invoking* it.

Is it another exclusive feature of javascript that saving a method in a variable invokes the method?

Saving a function in a variable, that is, assigning a function to a variable will not invoke the function. We need to write an expression that invokes it.

const myFunc = function (x) {  // assignment
    return x;
}
console.log(myFunc("I get returned to the caller"))  // invoke and log
// <- I get returned to the caller

let test = console.log(‘test’); invokes console.log function

I still don’t get it how
const removed = newItemTracker.pop();
invoked/called newItemTracker.pop()

The method is invoked in the assignment, and the last element value assigned to the removed variable. The parens do the job.

thanks for taking the time to answer!! really appreciate it

but the thing I don’t seem to understand is that below
const removed = newItemTracker.pop();

“newItemTracker.pop();” expression is on the right side, so whatever it does gets assigned to the left side “const removed” expression. After which the variable removed is equal to the last item. But am I getting it right that even if you write it on the right side, it still gets called. For example,
let x = f(y) not only assigns the value of f(y) to x but also calls f(x) ?

The pop method removes the last item from an array, and permits us to assign the singular value to a variable or use it in an expression.

a = "abcdefghijklmnopqrstuvwxyz".split("")
log = x => {
    console.log(x)
}
a.forEach(log)
console.log(a)

The array is still intact since all we did was iterate over it and log each element. Now let’s take it down.

a = "abcdefghijklmnopqrstuvwxyz".split("")
log = () => {
  console.log(a.shift())  // same as pop but from the front
}
a.slice().forEach(log)
console.log(a)

This is demonstration code, only to see the mechanics. We have decimated the original array, not as we iterate it, but as we iterate a copy. Nothing messes with a loop like removing elements during iteration.