Why the last item gets removed since we only “saved it” in a variable


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();

// Output: [ 'item 0', 'item 1' ]
// 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…



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.

1 Like

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