Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to find all elements in a given array except for the first one using JavaScript ?

  • Difficulty Level : Medium
  • Last Updated : 18 Aug, 2021

In this article, we will learn how to find all elements in a given array except for the first one.

Method 1: Using for loop
In this method, we will use a for loop to grab all the elements except first. We know that in an array the first element is present at index ‘0’. We will run a loop from 1 to array.length and saving those remaining elements to another array.

Example:

Javascript




<script>
    //Javascript program to find all
    //element of an array except first
 
    //function which takes an array as argument
    const print = (arr) => {
 
        //creating a dummy array to store result
        const find = []
 
        //a counter for adding result
        let k = 0
 
        for (let i = 1; i < arr.length; i++) {
            find[k] = arr[i]
            k++
        }
        //returning resultant array
        return find
    }
 
    //input array
    const arr = [1, 2, 3, 4, 5]
 
    //printing the result
    console.log(print(arr))
</script>

Output:

[ 2, 3, 4, 5 ]

Method 2: Using slice() method

The slice()It is a method that returns a slice of an array. It takes two arguments, the start and end index.

Example: 

Javascript




<script>
    //Javascript program to find all
    //element of array except first
 
    //function which takes an array as argument
    const print = (arr) => {
 
        //returning resultant array
        return arr.slice(1)
    }
 
    //input array
    const arr = [10, 20, 30, 40, 50]
    //printing the result
    console.log(print(arr))
</script>

Output :

[20, 30, 40, 50]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!