To remove the first and last array elements in JavaScript, we have tested effectively with three methods using the shift() and pop() method, using the slice() method, and using the splice() method. Check out the details below to get more information.
Remove the first and last array elements in JavaScript
Using the shift() and pop() method
The first way we want to introduce to you to remove the first and last array elements, call the shift() function to remove the first and the pop() method to remove the last element from an array.
Shift method syntax:
array.shift()
Parameter: NONE
Return value: the returns value is the removed item.
Pop method syntax:
array.pop()
Parameter: NONE
Return value: the returns value is the removed item.
Code example:
In this example, we use two very common methods in JavaScript.
- Firstly, we use the shift method to remove the first element in myArray. When we call this method, it will return the element to be removed.
- Secondly, we call the pop() method to remove the last element in myArray.
var myArray = ["Hello,", "I'm", "LearnShareIT", "!!!"]
console.log("My array: ", myArray)
// Using the shift method to remove the first element in myArray
myArray.shift()
// Using the pop() method to remove the last element in myArray
myArray.pop()
console.log("After removing the first and last element in myArray: ", myArray)
Output
My array: (4) ['Hello,', 'I'm', 'LearnShareIT', '!!!']
After removing the first and last element in myArray: (2) ['I'm', 'LearnShareIT']
Using the slice() method
The next way is using the slice() method to remove the first and last element in the array. We have already explained the slice() method in our other article here. Please read it if you want to get more information.
Example:
var myArray = ["Hello,", "I'm", "LearnShareIT", "!!!"]
console.log("My array: ", myArray)
// Using the slice() method to remove element with 1(first) and -1 (last)
var newArray = myArray.slice(1, -1)
console.log("After removing the first and last element in myArray: ", newArray)
Output
My array: (4) ['Hello,', 'I'm', 'LearnShareIT', '!!!']
After removing the first and last element in myArray: (2) ['I'm', 'LearnShareIT']
Using the splice method
The splice() method is already built-in in JavaScript. The developer can easily call it out and then use it.
Example:
var myArray = ["Hello,", "I'm", "LearnShareIT", "!!!"]
console.log("My array: ", myArray)
// Using splice(0, 1) method to remove the first element
myArray.splice(0, 1);
// Get the last element in Array
var getLastElement = myArray.length - 1;
// Calling the splice() method again to remove the last elements
myArray.splice(getLastElement, 1);
console.log("After removing the first and last element in myArray: ", myArray)
Output
My array: (4) ['Hello,', 'I'm', 'LearnShareIT', '!!!']
After removing the first and last element in myArray: (2) ['I'm', 'LearnShareIT']
Summary
Thank you for taking the time to read our full article. We have already explained how to remove the first and last array elements in JavaScript in many ways but using the slice() method is a very simplest and most useful way, and we hope the above information helps you in your learning process. You can visit our website to read more exciting topics.
Have a nice day, and leave your comment here if you have any questions!
Maybe you are interested:
- Get every Nth Element of Array in JavaScript
- Remove the first N elements from an Array using JavaScript
- Remove Undefined Values from an Array using JavaScript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python