Given an Array Arr with elements, the task is to remove the last item from an array in JavaScript. This can be done in two ways:
- Updating the original array: This method involves directly modifying the original array to remove the last item.
- Using a new array: This method involves creating a new array that excludes the last item from the original array.
Examples
Input: Arr = [ 10, 20, 30, 40, 50 ]
After removing the last element
Output: [ 10, 20, 30, 40 ]
Input: Arr = [ "Apple", "Banana", "Mango", "Greps" ]
After removing the last element
Output: [ "Apple", "Banana", "Mango" ]
Below, we will explain each approaches with a description and examples.
Remove Last Item from a JavaScript Array using Array.pop() Method
The pop() method is used to remove and return the last element from an array. First, we declare an Array Arr and then apply pop() method on Array i.e. Arr.pop() to remove the last element from an array. The Arr.pop() method returns the removed element and we can store it into the variable.
const Arr = [ 10, 20, 30, 40, 50 ]; const removedItem = Arr.pop(); // Output: [ 10, 20, 30, 40 ] console.log(Arr);
Remove Last Item from a JavaScript Array using Array.slice() Method
The slice() method is used to create a new array excluding the last item. Declare an Array Arr and use slice() method on Array i.e. Arr.slice(start, end) to create a new array with the selected elements. We use start index to 0 (array indexing starts with 0), and end index to -1 (negative indexing starts with the end of array).
const Arr = [ 10, 20, 30, 40, 50 ]; const newArr = Arr.slice(0, -1); // Output: [ 10, 20, 30, 40 ] console.log(newArr);
Remove Last Item from a JavaScript Array using Array.splice() Method
The splice() method is used to remove the last item from array by specifying the start index and the number of items to remove. The splice() method accepts two parameters, i.e. start index, and total number of item to remove. Here, we use start index to -1, i.e. last element of array, and item to removed 1. It means, remove the last element (remove 1 element from end of array) from an array.
const Arr = [ 10, 20, 30, 40, 50 ]; Arr.splice(-1, 1); // Output: [ 10, 20, 30, 40 ] console.log(Arr);
Remove Last Item from a JavaScript Array using Array.filter() Method
The filter() method is used to create a new array excluding the last item of existing array. The filter() method iterates through each element of the array, and the callback function returns true for all elements except the last one.
const Arr = [ 10, 20, 30, 40, 50 ]; const newArr = Arr.filter((_, index, arr) => index < arr.length - 1); // Output: [ 10, 20, 30, 40 ] console.log(newArr);
Remove Last Item from a JavaScript Array using Array.length Property
The length property returns the length of an array. First, declare an array Arr with some values, and then reduce the array length by 1 (Arr.length – 1) to remove the last item from array.
const Arr = [ 10, 20, 30, 40, 50 ]; Arr.length = Arr.length - 1; // Output: [ 10, 20, 30, 40 ] console.log(Arr);
Remove Last Item from a JavaScript Array using ES6 Destructuring
In JavaScript ES6, the array destructuring is used to create a new array without the last item. This code reverses the array Arr, destructures it to exclude the first element, and stores the remaining elements in newArr. Finally, it reverses newArr again to restore the original order minus the first element.
const Arr = [ 10, 20, 30, 40, 50 ]; const [, ...newArr] = Arr.reverse(); // Output: [ 10, 20, 30, 40 ] console.log(newArr.reverse());
Remove Last Item from a JavaScript Array using for Loop and Array.push() Method
Given an array with some values, we will create a new array newArr by iterating through the original array Arr using a for loop. It stops before the last element and pushes each element into newArr.
const Arr = [ 10, 20, 30, 40, 50 ]; const newArr = []; for (let i = 0; i < Arr.length - 1; i++) { newArr.push(Arr[i]); } // Output: [ 10, 20, 30, 40 ] console.log(newArr);