JavaScript

Remove the First Element From an Array in JavaScript

Developers frequently want to utilize the basic program code for removing entries from an array. Sometimes, there is a situation faced by programmers remove the first element from an array so that they can append the required element at the start. To do this, JavaScript offers some prebuilt methods, such as the shift() method and slice() method.

This post will demonstrate the methods for removing the first element from an array using JavaScript.

How to Remove the First/1st Element From an Array in JavaScript?

For removing the 1st element from an array, use the given JavaScript methods:

Method 1: Remove the 1st Element From an Array Using shift() Method

To remove the 1st element of an array, use the JavaScript prebuilt “shift()” method. It removes an array’s first element and returns it with shifted elements.

Syntax

Use the given syntax for removing the 1st element of an array:

array.shift()

Example

In the given example, we will create the following “array”:

var array = ['Java', 'JavaScript', 'HTML', 'CSS'];

Call the shift() method that will remove the first element of an array and store that element in a variable “removeFirstElement”:

var removeFirstElement = array.shift();

Print the resultant shifted array on the console:

console.log(array);

Also, print the element that has been removed from the array on the console:

console.log(removeFirstElement + " is removed from the Array");

The output indicates that the “Java” is successfully eliminated from the array:

Method 2: Remove the First Element From an Array Using slice() Method

Another method for removing the 1st element from an array is the “slice()” method. It returns the array of elements between the first and the last provided indexes.

Syntax

Follow the given syntax for a slice() method

slice(firstIndex, lastIndex)

In the above-given syntax:

  • firstIndex” is the index of the starting element where the array will split.
  • lastIndex” is the last index of an array for splitting.

Example

Call the “slice()” method by passing “1”, which is the first index of the array, to get the slice of the array starting from the 1st index by removing the element from the 0th index:

var removeFirstElement = array.slice(1);

Print the resultant array on the console:

console.log(removeFirstElement);

Output

That’s all about removing the 1st element from an array.

Conclusion

For removing the 1st element from an array, utilize the “shift()” method or the “slice()” method. The shift() method removes the first element and returns the array with shifted elements, while the slice() method slices the array by passing the start and the end indexes. In this post, we have demonstrated the methods for removing the 1st element from an array using JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.