Remove Elements from Array Start

Last Updated : 2 Feb, 2026

We will learn how to remove specific elements from the left side of a given array using JavaScript. This involves manipulating the array to exclude certain elements starting from the beginning.

  • Work with a given array of elements.
  • Remove specified elements starting from the left side of the array.

Approach 1: Using splice() method

The splice() method is used to add and remove elements from an array.

  • To remove elements from the left side of an array, the splice() method is applied starting from index 0.
  • It takes two parameters: the starting index and the number of elements to remove.
  • The method returns an array containing the removed elements.

Syntax:

array.splice(index,No_of_element);

We will remove specific elements from the left of a given array of elements using JavaScript using the splice() method.

JavaScript
function fun(n) {
    // Array
    var arr = [2, 4, 5, 3, 6];
    
    // Find index of specified element which is n
    var ind = arr.indexOf(n);
    
    // Remove n from array if it exists
    if (ind !== -1) {
        arr.splice(ind, 1);
    }
    
    // Log the results to the console
    console.log("After removing element:");
    console.log(arr);
}

// Call the function with the specified element to remove
fun(5);

Output
After removing element:
[ 2, 4, 3, 6 ]
  • Define a function fun(n) and create an array arr with predefined numeric values.
  • Use indexOf() to check if n exists in the array, and if found, remove it using splice().
  • Log a confirmation message and print the updated array, then call the function with 5 as the argument.

Approach 2: Using shift Method

The shift() method removes the first element from an array.

  • It returns the removed element.
  • This method directly modifies the original array.
  • The length of the array is reduced by one after removal.
  • It is a simple and effective way to remove an element from the beginning of an array.

We removes the first element from array using shift(), which modifies the array and returns the removed element. It then logs the updated array and the removedElement to the console.

JavaScript
let array = [1, 2, 3, 4, 5];
let removedElement = array.shift();

console.log(array); 
console.log(removedElement); 

Output
[ 2, 3, 4, 5 ]
1
  • Create an array containing elements from 1 to 5.
  • Use the shift() method to remove and store the first element in removedElement.
  • Log the updated array and the removed element (1) to the console.
Comment

Explore