Array unshift method in Javascript

‍Array unshift Method is the Javascript method that adds one or more elements to the beginning of an array and returns the new length of the array. This article will look closely at what the unshift method does, how it works, etc. We will learn how to add a single element or multiple items to an existing array, learn How to merge two arrays using the Unshift method, etc.

Array unshift Method

The array Unshift method is a built-in JavaScript method that adds new elements to the beginning of an array. It takes the element to be added as an argument and returns the array’s new length. This method can insert scalar values, objects, or another array into an array.

Syntax

The syntax for the unshift() method is as follows:

Here, the array is the array to which we want to add elements, and element1, element2, …, and elementN are the elements we want to add to the beginning of the array. Note that this method can add as many elements as we want.

Array unshift Examples

Let us look at some of the examples of the array unshift method.

Adding an item to the array

The code snippet that follows creates an array of numbers with four items. It then uses the unshift method to insert 1 to the beginning of the array.

Adding multiple items to the array

You can insert multiple elements to the array by supplying multiple items separated by a comma to the unshift method.

Merging arrays using unshift method

We can use unshift method to merge two arrays. The code below merges the nums array with the numbers array.

Note that we are not creating a new array but taking the value from the nums array and inserting it at the start of the numbers array.

The unshift method mutates the array

The unshift method mutates the array. It changes the length and content of the current array.

If you do not want to mutate the array (i.e., you want to return a new one instead of modifying the array), then you can use the concat method or the spread operator.

The following code uses the spread operator to insert a new value at the beginning of the array without modifying the original array.

The following code uses the concat method to create a new array and insert a value 4 at the beginning of the array

Vs. push

The push method is very similar to the unshift method. The difference between push & unshift is that the push method appends an element to the end of an array, while the unshift method inserts an element at the start of an array.

‍The following code uses the push method to push a value into the array. Note that it will insert the value at the end of the array.

Performance of unshift method

When we call unshift() on an array, it has to shift existing elements to a higher index to make room for the new elements.

Shifting elements is a time-consuming process. Its performance depends on the size of the array and the number of elements it will be moving. It also needs to assign a new index number to every element in the array.

Hence if you are working on a large array and do a unshift regularly, you need to rethink your data structure.

The push method is much faster than the unshift as it does not have to move the elements and assign a new index to them.

Array Like objects

The unshift is a generic method and will work on the array-like objects.

The code below applies the unshift method on an array-like object using the call function. As you can see, we insert value 1 at index 0 and push all the other numeric properties one index higher.

Summary

  • The array unshift method inserts an element (or elements) to the beginning of an Array.
  • The unshift method alters the original array. It changes its length and content. It returns the new length of the array
  • You can insert a single item or multiple items or merge two or more arrays.
  • Unshift is slower as it needs to move the entire array to make space for new elements.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top