Remove First n Elements Of An Array In JavaScript

To remove first n elements of an array in JavaScript, you can use any of the following methods –

  1. Use Array.splice() method and pass 0 as the first argument and n as the second argument. This will remove first n elements from the array and return the remaining array.
  2. Use Array.slice() method and pass 0 as the first argument and n as the second argument. This will return a new array containing first n removed elements.
  3. Use Array.shift() method in a loop to remove first n elements from the array. This method removes the first element of an array and returns that removed element.

Let’s discuss each of these methods in detail below.

remove first n elements of an array in javascript

Array.splice() To Remove First n Elements Of An Array In JavaScript

The Array.splice() method can be used to remove elements from an array as well as add new element(s) to the array. This method changes the contents of an array by removing existing element(s) and/or adding new element(s).

If you pass only two arguments to the splice() method, it will remove elements from the array starting at the index given by the first argument. The number of elements to remove is given by the second argument.

The first argument is required while the second and subsequent arguments are optional.

Syntax:

array.splice(start, deleteCount, item1, item2, …)

Parameters:

start – Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array.

deleteCount – An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.

item1, item2, … – The element(s) to add to the array, beginning at the start index. If you don’t specify any elements, splice() will only remove elements from the array.

Return value:

An array containing the removed elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

Example:

In the following example, splice() method is used to remove first two elements from the array arr and return the removed elements in an array.

var arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

var removed = arr.splice(0, 2);

console.log(removed); // [‘a’, ‘b’]

console.log(arr); // [‘c’, ‘d’, ‘e’]

Here is an example of passing only first argument to the splice() method. This will delete all elements of the array starting from the given index.

var arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

var removed = arr.splice(2);

console.log(removed); // [‘c’, ‘d’, ‘e’]

console.log(arr); // [‘a’, ‘b’]

Here is an example of passing the deleteCount greater than the number of elements in the array.

This will delete all elements from the given index to the end of the array.

var arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

var removed = arr.splice(2, 5);

console.log(removed); // [‘c’, ‘d’, ‘e’]

console.log(arr); // [‘a’, ‘b’]

Array.slice() To Remove First n Elements Of An Array In JavaScript

The Array.slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

If you omit the end argument, the slice extracts to the end of the array.

Syntax:

array.slice(begin, end)

Parameters:

begin – Zero-based index at which to begin extraction. As a negative index, begin indicates an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence.

If begin is undefined, slice begins from index 0.

end – Zero-based index at which to end extraction. slice extracts up to but not including end. slice(1,4) extracts the second element up to the fourth element (elements indexed 1, 2, and 3).

As a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts the third element through the second to last element in the sequence.

If end is omitted, slice extracts through the end of the sequence (arr.length).

If either argument is greater than the array’s length, it acts as if it were equal to the length.

Returns:

A new array containing the extracted elements.

Example:

In the following example, slice() method is used to remove first two elements from the array arr and return the removed elements in an array.

var arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

var removed = arr.slice(0, 2);

console.log(removed); // [‘a’, ‘b’]

console.log(arr); // [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Here is an example of passing only first argument to the slice() method. This will delete all elements of the array starting from the given index.

var arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

var removed = arr.slice(2);

console.log(removed); // [‘c’, ‘d’, ‘e’]

console.log(arr); // [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Here is an example of passing the end greater than the number of elements in the array. This will delete all elements from the given index to the end of the array.

var arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];

var removed = arr.slice(2, 5);

console.log(removed); // [‘c’, ‘d’, ‘e’]

console.log(arr); // [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

Array.shift() To Remove First n Elements Of An Array In JavaScript

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

Syntax:

array.shift()

Returns: The removed element from the array. If the array is empty, it returns undefined.

Example: In the following example, shift() method is used to remove first element from the array arr and return the removed element.

var arr = [‘a’, ‘b’, ‘c’];

var removed = arr.shift();

console.log(removed); // a

console.log(arr); // [‘b’, ‘c’]

Use While loop

You can use a while loop to remove first n elements of an array in JavaScript. In the following example, a while loop is used to delete the first two elements of the array arr.

var arr = [‘a’, ‘b’, ‘c’];

var n = 2;

while (n--) {

  arr.shift();

}

console.log(arr); // [‘c’]

Use for loop

You can also use a for loop to remove first n elements of an array in JavaScript. In the following example, a for loop is used to delete the first two elements of the array arr.

var arr = [‘a’, ‘b’, ‘c’];

for (var i = 0; i < 2; i++) {

  arr.shift();

}

console.log(arr); // [‘c’]

Conclusion

In this article, you learned how to remove first n elements of an array in JavaScript. You can use either of the following methods to achieve this:

  1. Array.splice() method
  2. Array.slice() method
  3. Array.shift() method with while loop
  4. Array.shift() method with for loop

I hope this article was helpful. Thank you for reading!

Leave a Reply