How to Pop the Last Element in a JavaScript Array?

Estimated read time 1 min read

To pop the last element in a JavaScript array, you can use the pop() method. This method removes the last element from the array and returns that element. Here’s an example:

let arr = [1, 2, 3, 4];
let lastElement = arr.pop();

console.log(lastElement); // outputs 4
console.log(arr); // outputs [1, 2, 3]

In this example, pop() is called on the arr array, and the last element (4) is removed and stored in the lastElement variable. The resulting array is [1, 2, 3].

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply