Skip to content
Related Articles
Open in App
Not now

Related Articles

How to remove n elements from the end of a given array in JavaScript ?

Improve Article
Save Article
  • Last Updated : 14 Dec, 2022
Improve Article
Save Article

In this article, we will learn how to remove the last n elements from the end of the given array in JavaScript. This can be achieved using three approaches using javaScript splice() method, pop() method, and filter() method.

Javascript splice() Method: It is used to modify an array by adding or removing elements from it. This method accepts the index from which the modification has to be made and the number of elements to delete. The index from which the deletion has to start can be found by subtracting the number of elements from the length of the array. 

Syntax:

array.splice(start, deleteCount);

Example: In this example, we will be using the Javascript splice() method to remove the elements from the end of a given array.

Javascript




<script>
    // Define the array
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
     
    console.log("Original Array:", arr);
     
    // Define the number of elements to remove
    let elemsToDelete = 3;
     
    // Using the splice() method to remove from
    // the last nth index for n elements
    arr.splice(arr.length - elemsToDelete,
        elemsToDelete);
     
    console.log("Modified Array:", arr);
</script>

Output:

Original Array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Modified Array: [1, 2, 3, 4, 5, 6]

Javascript pop() Method: It is used to remove the last element from the array. This can be repeated in a loop of n iterations to remove the last n elements of the array using the while loop.

Syntax:

array.pop();

Example: In this example, we will be using the Javascript pop() method to remove the elements from the end of a given array.

Javascript




<script>
    // Define the array
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
     
    console.log("Original Array:", arr);
     
    // Define the number of elements to remove
    let elemsToDelete = 5;
     
    // Loop for the number of elements
    // to delete
    while (elemsToDelete--)
     
        // Pop the last element from the
        // end of the array
        arr.pop();
     
    console.log("Modified Array:", arr);
</script>

Output:

Original Array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Modified Array: [1, 2, 3, 4]

Javascript filter() Method: It is used to filter the array and apply the callback function to each item of the array and filter the element which returns true against the callback function.

Syntax: 

Array.filter( callback )

Example: In this example, we will be using the Javascript filter() method to remove the elements from the end of a given array.

Javascript




<script>
    // Define the array
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
     
    console.log("Original Array:", arr);
     
    // Define the number of elements to remove
    let elemsToDelete = 5;
     
    // Loop for the number of elements
    // to delete
    let k = arr.filter((x, i) => i + elemsToDelete < arr.length)
     
    // Pop the last element from the
    // end of the array
    console.log("Modified Array:", k);
</script>

Output:

Original Array: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Modified Array: [ 1, 2, 3, 4 ]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!