How to Remove First and Last Element from Array using JavaScript?
Last Updated :
17 Jul, 2024
Removing the first and last elements from an array is a common operation. This can be useful in various scenarios, such as data processing, filtering, or manipulation.
Example:
Input: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
Output: [ 2, 3, 4, 5, 6, 7, 8, 9 ]
Removing first and last element in array can be done using below approaches:
Using Slice() Method
The approach involves creating a new array by extracting a portion of an existing array. The slice() method is used to take elements starting from the second position up to the second-to-last position. This excludes the first and last elements of the original array. The resulting new array contains all the elements from the second to the penultimate one of the original array.
Example: This example shows that the slice() method can be used to remove the first and last elements from an array.
JavaScript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let newArray = arr.slice(1, -1);
console.log(newArray);
Output[
2, 3, 4, 5,
6, 7, 8, 9
]
Using Splice() Method
The approach involves populating an array with a sequence of numbers and then removing the first and last elements if the array contains more than one element. The array is initially empty and numbers from 0 to 9 are added to it. After ensuring the array has more than one element, the splice() method is used to remove the first element and then the last element. The resulting array excludes the original first and last elements.
Example: This example shows that the splice() method can be used to remove elements from an array.
JavaScript
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
if (arr.length > 1) {
arr.splice(0, 1);
arr.splice(arr.length - 1, 1);
}
console.log(arr);
Output[
2, 3, 4, 5,
6, 7, 8, 9
]
Similar Reads
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
JavaScript - How to Get First N Elements from an Array? There are different ways to get the first N elements from array in JavaScript.Examples:Input:arr = [1, 2, 3, 4, 5, 6], n = 3 Output: [1, 2, 3] Input:arr = [6, 1, 4, 9, 3, 5, 7], n = 4Output: [6, 1, 4, 9]1. Using slice() MethodThe slice() method is used to extract a part of an array and returns a new
4 min read
How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt
3 min read
Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple",
6 min read
How to delete an element from an array using JavaScript ? The array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index. In this article, we will discuss different ways to remove elements from the array.
5 min read