The Wayback Machine - https://web.archive.org/web/20241001013152/https://www.geeksforgeeks.org/javascript-array-unshift-method/
Open In App

JavaScript Array unshift() Method

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript Array unshift() Method is used to add one or more elements to the beginning of the given array. This function increases the length of the existing array by the number of elements added to the array.

Syntax:

array.unshift(element1, element2, ..., elementX);

Parameters:

  • element: This parameter element is to be added at the beginning of the array.

Return value:

This function returns the new length of the array after inserting the arguments at the beginning of the array. 

Example 1: Below is an example of the Array unshift() method.

JavaScript
function func() {

    // Original array
    let array = ["GFG", "Geeks", "for", "Geeks"];

    // Checking for condition in array
    let value = array.unshift("GeeksforGeeks");
    console.log(value);
    console.log(array);
}
func();

Output
5
[ 'GeeksforGeeks', 'GFG', 'Geeks', 'for', 'Geeks' ]

Example 2: In this example, the function unshift() adds 28 and 65 to the front of the array.

JavaScript
function func() {
    let arr = [23, 76, 19, 94];
    
    // Adding elements to the front of the array
    console.log(arr.unshift(28, 65));
    console.log(arr);
}
func();

Output
6
[ 28, 65, 23, 76, 19, 94 ]

Example 3: In this example, the unshift() method tries to add the element of the array, but the array is empty therefore it adds the value in the empty array.

JavaScript
function func() {
    let arr = [];
    console.log(arr.unshift(1));
}
func();

Output
1

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Opera 4
  • Safari 1

JavaScript Array unshift() Method – FAQs

What does the Array.prototype.unshift() method do in JavaScript?

The Array.prototype.unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

Can unshift() be used to add multiple elements at once?

Yes, you can add multiple elements at once by passing them as arguments to unshift(). For example: array.unshift(element1, element2, element3).

How does unshift() affect the indices of existing elements in the array?

The indices of the existing elements in the array are incremented to accommodate the new elements added to the beginning.

Can unshift() handle non-numeric elements?

Yes, unshift() can handle elements of any type, including strings, objects, arrays, and other primitive values.

What are common use cases for the unshift() method?

  • Adding Elements to the Beginning: Using unshift() to prepend elements to an array.
  • Updating Queue-like Structures: Implementing or modifying data structures like queues where elements need to be added to the front.
  • Reversing Order of Operations: Ensuring new elements are processed first by adding them to the beginning of an array.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Previous Article
Next Article

Similar Reads

How to perform unshift operation without using unshift() method in JavaScript?
Performing an unshift operation in JavaScript without unshift() involves using array concatenation or the ES6 spread operator. These methods create a new array by prepending elements, achieving the same result as unshift() without directly using it. Table of Content Using Array concat() methodUsing ES6 Spread OperatorUsing Array concat() MethodIn t
2 min read
How the Array.unshift() method works in JavaScript ?
The Array.unshift() method in JavaScript is used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array after the elements have been added. Key points about Array.unshift(): It modifies the original array and returns the new length.It can accept multiple arguments, and each a
1 min read
What is the use of the Array.unshift() method in JavaScript ?
In JavaScript, the Array.unshift() method is used to add one or more elements to the beginning of an array. It modifies the original array by adding the specified elements at the front, shifting existing elements to higher indices to accommodate the new elements. Syntax:array.unshift(element1, element2, ..., elementN)Example: Here, we have an array
1 min read
What is the difference between unshift() and Push() method in JavaScript?
JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array. Javascript Array unshift() method: The JavaScript Array unshift() Method is used to add one or more elements to the beginni
2 min read
TypeScript Array unshift() Method
The Array.unshift() method in TypeScript is an inbuilt function used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array. Syntaxarray.unshift( element1, ..., elementN )ParameterThis method accepts n number of similar elements. element1, ..., elementN : This parameter is th
2 min read
Node.js Stream readable.unshift() Method
The readable.unshift() method in a Readable Stream is utilized to push a chunk of data back into the internal buffer. However, when a stream is being fully consumed and it needs to be "un-consumed" then this method is helpful. Syntax: readable.unshift( chunk, encoding ) Parameters: This method accept two parameters as mentioned above and described
2 min read
PHP | Ds\Vector unshift() Function
The Ds\Vector::unshift() function is an inbuilt function in PHP which is used to adds elements to the front of vector. This function moves all the elements in the vector towards forward and adds the new element to the front. Syntax: void public Ds\Vector::unshift( $values ) Parameters: This function accepts single parameter $values which holds the
2 min read
PHP | Ds\Sequence unshift() Function
The Ds\Sequence::unshift() function is an inbuilt function in PHP which is used to add values to the font of the sequence. Syntax: void abstract public Ds\Sequence::unshift( $values ) Parameters: This function accepts a single parameter $values which contains values to add in the font of the sequence. Return value: This function does not return any
1 min read
PHP | Ds\Deque unshift() Function
The Ds\Deque::unshift() function is an inbuilt function in PHP which is used to add the value in front of the deque. Syntax: void Ds\Deque::unshift( $values ) Parameters: This function accepts single parameter $values which holds the values to add in the front of the deque. Return Value: This function does not return any values. Below programs illu
2 min read
PHP SplDoublyLinkedList unshift() Function
The SplDoublyLinkedList::unshift() function is an inbuilt function in PHP which is used to add the element at the beginning of a doubly linked list. Syntax: void SplDoublyLinkedList::unshift( $value ) Parameters: This function accepts a single parameter $value which is used to unshift the value in a doubly linked list. Return Value: It does not ret
2 min read
Node.js unshift() function
unshift() is an array function from Node.js that is used to insert elements to the front of an array. Syntax: array_name.unshift(element) Parameter: This function takes a parameter that has to be added to the array. It can take multiple elements also as parameters. Return type: The function returns the array after adding the element. The program be
1 min read
Backbone.js unshift Collection
The Backbone.js unshift Collection is used to add model at the beginning of a Collection. It takes model as the first parameter and options as the second parameter. In this article, we will discuss the Backbone.js unshift Collection. Syntax: collection. unshift( model, options ); Parameters: model: This parameter is used to specify the model's inst
2 min read
How to extend an existing array with another array without creating a new array in JavaScript ?
JavaScript arrays are a powerful data structure that allows you to store and manipulate collections of data. Often, we may need to combine or extend two or more arrays into a single array. It's easy to create a new array by concatenating two or more arrays but if we want to extend an existing array without creating a new array. There are two easy w
2 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery
3 min read
How does Promise.all() method differs from Promise.allSettled() method in JavaScript ?
In this article, we will first understand in brief the Promise.all() as well as Promise.allSettled() methods and then we will try to visualize how they differ from each other through some theoretical as well as some coding examples. Both Promise.all() and Promise.allSettled() methods are the methods of a Promise object (which is further a JavaScrip
4 min read
How does Promise.any() method differs from Promise.race() method in JavaScript ?
In this article, we will first try to understand how we may declare or use Promise.any() and Promise.race() methods followed by some facts which will eventually help us to understand how they differ from each other (through theoretical as well as coding examples). Let us first quickly understand in brief about both the methods followed by their syn
5 min read
How toReversed() method is different from reverse() method in JavaScript
In JavaScript, reverse() and toReversed() methods do the same job as reversing but toReversed() does not change the original array. The reverse() method changes the original array. Below we have explained both methods. Table of Content Reverse() MethodtoReversed() methodReverse() MethodThe reverse() method is a built-in method for arrays in JavaScr
2 min read
Implement polyfill for Array.prototype.map() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or because the new version of the br
3 min read
JavaScript Array map() Method
JavaScript Array map() Method method creates a new array with the results of a called function for every array element. This function calls the argument function once for each element of the given array in order. Syntax: // Arrow function map((element) => { /* … */ }) map((element, index) => { /* … */ }) map((element, index, array) => { /*
3 min read
JavaScript Array valueOf() Method
The JavaScript Array valueOf() method in JavaScript is used to return the array. It is a default method of the Array Object. This method returns all the items in the same array. It will not change the original content of the array. It does not contain any parameter values. Syntax: array.valueOf()Parameters: This method does not accept any parameter
2 min read
How to use Array.prototype.reduce() method in JavaScript ?
The Array.prototype.reduce() method is used in an array to return a single value from an array after executing the user-supplied callback function on each element of the array. It transverses from the left-most-element to the right-most-element of the given array. Array.prototype.reduce() can be called using two ways. Callback function: The functio
3 min read
Which one method use to delete an element from JavaScript array (slice or delete) ?
We can use both methods to delete an element from a javascript array. The answer is solely dependent on the application and on the programmer. What slice() method does? The slice method, as the name suggests, slices an array and returns a copy of the new array. This method does not affect the main array, it just returns the copy of the array. We ca
4 min read
Which built-in method removes the last element from an array and returns that element in JavaScript ?
In this article, we will know how to remove the last element from an array using the built-in method in Javascript, along with understanding its implementation through the examples. There are a few Javascript built-in methods and techniques with which we can remove the last element of the array. We will focus only on the 2 methods ie., the pop() me
3 min read
Implement polyfill for Array.prototype.reduce() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.reduce() method in JavaScript. A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or the new version of the browser does not have that
2 min read
JavaScript Array findLastIndex() Method
The Javascript findLastIndex() method is used to find the index of the last element in an array that matches the specified condition. It works by iterating over the array from the end to the beginning and returning the index of the first element that satisfies the condition specified in a callback function. If no element matches the condition, the
2 min read
What is the use of the Array.from() method in JavaScript ?
In JavaScript, the Array.from() method is used to create a new array instance from an iterable object or array-like object. It provides a way to convert objects that are not inherently arrays into actual arrays. Example: Here we see that output creates a new array whose content is the same as input in the case of an integer. C/C++ Code console.log(
1 min read
What is the use of the Array.some() method in JavaScript ?
The Array.some() method in JavaScript is used to check if at least one element in an array satisfies a given condition. It returns true if at least one element in the array passes the test implemented by the provided function otherwise, it returns false. Example: Here, the condition in the some() method checks if there is at least one element great
1 min read
What is the use of the Array.of() method in JavaScript ?
The JavaScript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method. Example: Here, Array.of() method is used to create new array instances with a variable number of arguments. C/C++ Code // Here the Array.of() method creates a new Array instance with // a variab
1 min read
What is Array.prototype.slice() method in JavaScript ?
The Array.prototype.slice() method in JavaScript is used to extract a portion of an array and create a new array containing the selected elements. It does not modify the original array. instead, it returns a shallow copy of a portion of the array. Syntax:array.slice(startIndex, endIndex);Parameters:startIndex: The index at which to begin extraction
1 min read
What is the use of the Array.findIndex() method in JavaScript ?
The Array.findIndex() method in JavaScript is used to find the index of the first element in an array that satisfies a provided testing function. It returns the index of the first element for which the testing function returns true. If no such element is found, it returns -1. Syntax:array.findIndex(function(currentValue, index, arr), thisValue);Par
2 min read