Open In App

Delete the first element of array without using shift() method in JavaScript

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array containing some array elements and the task is to remove the first element from the array and thus reduce the size by 1. We are going to perform shift() method operation without actually using it with the help of JavaScript.

There are two approaches that are discussed below: 

Method 1: Using splice() Method

We can use the splice() method that is used to get the part of the array.

Example: In this example, we will implement the above approach.

Javascript




let Arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
 
console.log("Array: [" + Arr + "]");
 
function myGFG() {
    Arr.splice(0, 1);
    console.log("Elements of Array: [" + Arr + "]");
}
 
myGFG();


Output

Array: [Geeks,GFG,Geek,GeeksForGeeks]
Elements of Array: [GFG,Geek,GeeksForGeeks]

Method 2: Using filter() Method

We can use the filter() method to filter out the element at index 0.

Example: In this example, we will implement the above approach.

Javascript




let Arr = ['Geeks', 'GFG', 'Geek', 'GeeksForGeeks'];
 
console.log("Array: [" + Arr + "]");
 
function removeFirst(element, index) {
    return index > 0;
}
 
function myGFG() {
    Arr = Arr.filter(removeFirst);
    console.log("Elements of array = [" + Arr + "]");
}
 
myGFG();


Output

Array: [Geeks,GFG,Geek,GeeksForGeeks]
Elements of array = [GFG,Geek,GeeksForGeeks]


Last Updated : 30 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads