Unshift JavaScript Array Method – [With Examples]

Unshift is a built-in JavaScript array function. Unshift JavaScript adds one or more items to the beginning of an array of elements and returns a new array. The Unshift JavaScript method will modify the length of an array based on the number of items we added to it using the Method Unshift JavaScript Array. Chrome 1.0, Internet Explorer or Edge 5.5, Firefox Mozilla 1.0, Safari, and Opera are browsers that implement the Unshift JavaScript Array function. In Internet Explorer or Edge 8 and later, the Unshift JavaScript Array Method will return undefined.

You can pass as many arguments as you like to the unshift() method. All of these arguments will be append to the beginning of the array.

Syntax:

The Unshift JavaScript method accepts just one item as an argument. These array elements should be added to the array’s beginning. This function will return a new length array after adding the values at the beginning of the array.

Unshift JavaScript Array Method syntax_unshift

Examples of Unshift JavaScript Array Method

Example 01:

In this example, we’ll make an array of names. We’ll print the length of the array before using the unshift JavaScript function, and then we’ll publish the values of the original array. Then, on the newly formed array, we’ll call the unshift() function. Then, after executing the unshift() function, we will output the length of the array, followed by the updated array.

Unshift JavaScript Array Method example1_unshift

Output:

example1_unshift_output

Example 02:

John, a student of our class, had a fantastic . He got 1st position in whole class. We’d want to move John position at the top of our top class name array. To complete this task, we may use the following code:

example2_unshift

Output:

example2_unshift_output

As you can see, John’s name has been put to the beginning of our top_students list. The unshift() method operates similarly to the shift() function, with two exceptions: unshift() adds an item to the beginning of a list and accepts that item as an argument.

Difference Between Shift and Unshift in JavaScript

JavaScript shift() removes an element from a given location and pushes the other elements up. The unshift() function in JavaScript accomplishes the inverse. unshift() inserts a new element at a certain place in the list and moves the other elements down the list.

You may opt to add or delete an item from the beginning of a list. Assume you’ve compiled a list of the best salesmen. You should put the number one best-seller at the top of the list.

This is where JavaScript’s shift() and unshift() methods come in. The functions shift() and unshift() are used to remove and add items to the beginning of a list, respectively. These methods are similar to push() and pop(), except shift() and unshift() only enable removing and adding items to the beginning of a list.

That’s all for this article if you have any queries please contact us through our website or email us at [email protected]

Leave a Comment