Arrays are very important data types in almost all programming languages. Usually, programmers have to deal with a lot of variables and values while writing the codes for production. Still, it is much more convenient to use structures that can actually hold similar types of data. For example, suppose we need to store the salary of all the staff in the office. One way to do this is to create a separate variable for all the stuff in the office. Another approach is to make a data structure that can store all the salary information in one variable. The latter is much more convenient to use and hence we use the array.
Remove Last Item(Element) From an Array in JavaScript
There are multiple ways to remove last item(element) from Array in JavaScript.
- Use pop() Method
- Use slice() Method
- Use Third-party libraries(lodash or underscore)
Use pop() Method
There is a user-defined function in javascript that is used to remove the last item from an array in JavaScript.This is the pop() method. This method returns the removed element. Also, it changes the original array.
Syntax
arrayName.pop();
Return value
the last item in the array
Example
function remove_last_element(arr){
const last_element=arr.pop();
console.log(last_element);
}
a=[76,46,7,6,6,433,4,7];
remove_last_element(a);
console.log(a);
Output
7
[
76, 46, 7, 6,
6, 433, 4
]
Explanation
The above code can be explained as follows:
- We first created a user-defined function called remove_last_element. It takes only one parameter named arr. This is of array data type.
- Next, we created another variable named last_name. We used the pop function to remove the last element of the array and return the same. Hence the variable last_elemenet got the value of the last element of the array.
- We printed the output in the console.
- We created an array named a.Then we called the function remove_last_element to remove the last element.
Use Array.splice() Method
We can also apply logic to remove the last element of the array. We can use the slice() method of javascript for the same. We need to pass -1 as the argument. By default, the backcountry of the arrays starts from -1. So if -1 is sliced then only the last element is removed and the others are unaffected. This method returns a copy of the original array except the last element.
Syntax
splice(-1);
Return
the array with the last element removed
Example
a=[76,46,7,6,6,433,4,7];
a.splice(-1);
console.log(a);
Output
[
76, 46, 7, 6,
6, 433, 4
]
Using lodash Library
Javascript also offers many other third-party libraries to be used for removing the last element of the array. We need to first import the lodash library. Then we need to use the initial() method to remove the last element of the array.
Example
let load=require('lodash');
let arr=[37,56,2,4,22,43,23];
modified_array=load.initial(arr);
console.log(modified_array);
Output
[ 37, 56, 2, 4, 22, 43 ]
Explanation
The above code can be explained as follows:
- We first imported the library called the lodash using the require method.
- Now we created a user-defined array.
- Next we created another variable named modified_array and we used the initial() function of the lodash library.
- This function will automatically remove the last element from the array.
- Finally we printed the modified_array array in the console.
Using underscore library
Another method to remove the last element of the array is to use the underscore library. Again we need to import the library. We then need to use the initial function. This takes two arguments first the array and secondly the number of elements from last that need to be removed. So if we pass 1 as the second argument then we can get the last element removed.
Example
let underscore=require('underscore');
let arr=[37,56,2,4,22,43,23];
let n=1;
modified_array=underscore.initial(arr,n);
console.log(modified_array);
Output
[ 37, 56, 2, 4, 22, 43 ]
Explanation
The above code can be explained as follows:
- We first imported the required library underscore using the require method.
- Now we have created an array.
- Next, we used the initial() function and passed the array and n as the arguments.
- Finally, we printed the array in the console.
Conclusion
In this article, we have learned how to remove the last item(element) from an Array in JavaScript. There are many ways to achieve the same. However, the most efficient and effective way to remove the last element of the array is to use the pop() function.

Rushabh Rupani is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. Rushabh has experience with various programming languages and technologies, including PHP, Python, and expert in JavaScript. He is comfortable working in front-end and back-end development.