As you know, arrays have the 0-based index and if you know the index of a item, you can easily remove it from the array. However, when it comes to removing the first and last item, we can utilize built-in methods shift() and pop(). The shift() method will remove the first item in the array and the pop() method will remove the last item in the array.
In the following example, we have global array users and upon button click, I want to remove the first and last items. There are 2 buttons, one button is to remove the first item and another button is to remove the last item. Please have a look over the code example and steps given below.
HTML & CSS
We have 4 elements in the HTML file (div, 2 button, and ul). The div element is just a wrapper for the rest of the elements.
The first button element has “First” and the second button element has “Last” as inner text.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.