Remove the last N elements from an Array using JavaScript

You will know how to remove the last N elements from an Array using JavaScript via a variety of methods, namely slice() and pop(). These important skills must be learned if you are working with arrays in Javascript.

Remove the last N elements from an Array using JavaScript

Using slice()

The slice() method will return an array of N elements in an array starting from a specific index given

Syntax:

slice(start, count)

Parameter:

  • start: The starting point (index), in this case is zero
  • count: The number of how many elements you want to remove

You may think that this method is meant to return the first N element from an array. But this method also can do the reverse, it can remove the last N elements from an array also. For example:

let array = ['We','Are','Learn','Share','IT'];

// Remove the last 3 elements from an Array
let res = array.slice(0,-3);
console.log (res);
console.log(array);

Output:

['We', 'Are']
['We', 'Are', 'Learn', 'Share', 'IT']

The above example shows that we have an array of 5 elements and we want to remove the last 3 elements from that array. We have used the slice() method with zero as a start index and the count parameter equals to -3. It is not an invalid or incorrect number, the minus (-) sign implies that we want to remove 3 elements before the starting index (0 as declared). If you don’t have a negative sign, it will remove the first 3 elements (3 elements after starting point) instead.

Using pop() with a loop

The method pop() will remove a last element (only one) from an array.

Syntax:

pop()

You can create a for loop which runs N times; each time, it will call the method pop() on the array. So after N times, we have an array with the last N elements removed:

let array = ['We', 'Are', 'Learn', 'Share', 'IT'];

// Remove the last 3 elements from an Array
for (let i = 1; i <= 3; i++) {
    array.pop();
}

console.log(array);

Output: 

['We', 'Are']

The logic behind this approach is very easy to acknowledge. Because we want to remove the last 3 elements from an array, we try to remove the very last element in the array 3 times. This task can be done using a loop, and a for loop is the best choice for this purpose. However, changing or removing elements in an array, such as using the pop() method to remove the last element in the array, often causes issues when reasoning through code. This is because of the difficulties involved in debugging and understanding these code errors. Creating a duplicate of the array with only the necessary elements as in the first solution (using slice() method) is often the better option.

Summary

We have learned to remove the last N elements from an Array using JavaScript using two different approaches. We recommend you use the first method (using slice() method) as it is quick and safe. With the use of our guide in this tutorial and others, you can be successful in achieving the task.

Leave a Reply

Your email address will not be published. Required fields are marked *