This post will discuss how to remove the last element from an array in JavaScript.

1. Using Array.prototype.pop() function

The standard way to remove the last element from an array is with the pop() method. This method works by modifying the original array. The following code creates an array containing five elements, then removes its last element.

Download  Run Code

2. Using Array.prototype.splice() function

The splice() method is often used to in-place remove existing elements from the array or add new elements to it. The following example demonstrates the usage of the splice() to remove the last element from the array of length 5.

Download  Run Code

 
You can easily extend the above code to remove the last n elements from the array:

Download  Run Code

3. Using Lodash Library

If you’re using the Lodash JavaScript library in your project, you can use the initial() method, which returns everything but the last element of the array. Note that this doesn’t modify the original array but returns a new array.

Download Code

4. Using Underscore Library

Alternatively, to remove the last n elements from the array, pass n as the second parameter to initial() method of Underscore library.

Download Code

That’s all about removing the last element from an array in JavaScript.