Search code examples
javascriptarrays

to remove first and last element in array


How to remove first and last element in an array?

For example:

var fruits = ["Banana", "Orange", "Apple", "Mango"];

Expected output array:

["Orange", "Apple"]

Solution

  • fruits.shift();  // Removes the first element from an array and returns only that element.
    fruits.pop();    // Removes the last element from an array and returns only that element. 
    

    See all methods for an Array.