Use unshift. It's like push, except it adds elements to the beginning of the array instead of the end.

  • unshift/push - add an element to the beginning/end of an array
  • shift/pop - remove and return the first/last element of an array

A simple diagram...

unshift -> [array] <- push
shift   <- [array] -> pop

and chart:

  add remove start end
push X X
pop X X
unshift X X
shift X X

Check out the MDN Array documentation. Virtually every language that has the ability to push/pop elements from an array will also have the ability to unshift/shift (sometimes called push_front/pop_front) elements, you should never have to implement these yourself.


As pointed out in the comments, if you want to avoid mutating your original array, you can use concat, which concatenates two or more arrays together. You can use this to functionally push a single element onto the front or back of an existing array; to do so, you need to turn the new element into a single element array:

const array = [3, 2, 1]

const newFirstElement = 4

const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]

console.log(newArray);

concat can also append items. The arguments to concat can be of any type; they are implicitly wrapped in a single-element array, if they are not already an array:

const array = [3, 2, 1]

const newLastElement = 0

// Both of these lines are equivalent:
const newArray1 = array.concat(newLastElement) // [ 3, 2, 1, 0 ]
const newArray2 = array.concat([newLastElement]) // [ 3, 2, 1, 0 ]

console.log(newArray1);
console.log(newArray2);

Answer from user229044 on Stack Overflow
Top answer
1 of 12
4058

Use unshift. It's like push, except it adds elements to the beginning of the array instead of the end.

  • unshift/push - add an element to the beginning/end of an array
  • shift/pop - remove and return the first/last element of an array

A simple diagram...

unshift -> [array] <- push
shift   <- [array] -> pop

and chart:

  add remove start end
push X X
pop X X
unshift X X
shift X X

Check out the MDN Array documentation. Virtually every language that has the ability to push/pop elements from an array will also have the ability to unshift/shift (sometimes called push_front/pop_front) elements, you should never have to implement these yourself.


As pointed out in the comments, if you want to avoid mutating your original array, you can use concat, which concatenates two or more arrays together. You can use this to functionally push a single element onto the front or back of an existing array; to do so, you need to turn the new element into a single element array:

const array = [3, 2, 1]

const newFirstElement = 4

const newArray = [newFirstElement].concat(array) // [ 4, 3, 2, 1 ]

console.log(newArray);

concat can also append items. The arguments to concat can be of any type; they are implicitly wrapped in a single-element array, if they are not already an array:

const array = [3, 2, 1]

const newLastElement = 0

// Both of these lines are equivalent:
const newArray1 = array.concat(newLastElement) // [ 3, 2, 1, 0 ]
const newArray2 = array.concat([newLastElement]) // [ 3, 2, 1, 0 ]

console.log(newArray1);
console.log(newArray2);

2 of 12
1761

var a = [23, 45, 12, 67];
a.unshift(34);
console.log(a); // [34, 23, 45, 12, 67]

People also ask

What is the fastest way to add an element to the beginning of a JavaScript array?
For arrays with fewer than 10,000 elements, unshift() provides the fastest performance when mutation is acceptable. For immutable operations, the spread operator offers the best combination of performance and readability. For very large arrays (100,000+ elements), consider using concat() or building a new array with a loop.
๐ŸŒ
sencha.com
sencha.com โ€บ home โ€บ blog โ€บ how to add elements to the beginning of an array in javascript (2026 guide)
How to Add Elements to the Beginning of an Array in JavaScript ...
Does unshift() modify the original array?
Yes, unshift() modifies the original array in place. It adds elements to the beginning and shifts existing elements to higher indices. If you need to preserve the original array, use the spread operator or concat() to create a new array instead.
๐ŸŒ
sencha.com
sencha.com โ€บ home โ€บ blog โ€บ how to add elements to the beginning of an array in javascript (2026 guide)
How to Add Elements to the Beginning of an Array in JavaScript ...
What is the difference between push() and unshift()?
push() adds elements to the end of an array, while unshift() adds elements to the beginning. Both methods mutate the original array and return the new length.
๐ŸŒ
sencha.com
sencha.com โ€บ home โ€บ blog โ€บ how to add elements to the beginning of an array in javascript (2026 guide)
How to Add Elements to the Beginning of an Array in JavaScript ...
๐ŸŒ
Sencha
sencha.com โ€บ home โ€บ blog โ€บ how to add elements to the beginning of an array in javascript (2026 guide)
How to Add Elements to the Beginning of an Array in JavaScript (2026 Guide)
March 14, 2024 - The unshift() method is a built-in JavaScript Framework that adds one or more elements to the beginning of an array and returns the new length of the array.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_unshift.asp
JavaScript Array unshift() Method
โฎ Previous JavaScript Array Reference Next โฏ ยท Add new elements to an array: const fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.unshift("Lemon","Pineapple"); Try it Yourself ยป ยท The unshift() method adds new elements to the ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ add-new-elements-at-the-beginning-of-an-array-using-javascript
Insert at the Beginning of an Array in JavaScript - GeeksforGeeks
July 11, 2025 - Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element at the beginning of the array.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ add-element-to-start-of-an-array
JavaScript Program to Add Element to Start of an Array | Vultr Docs
September 27, 2024 - Use unshift() to add multiple elements at a time to the array's start. ... The example demonstrates adding both '1' and '2' to the beginning of the numbers array.
๐ŸŒ
Futurestud.io
futurestud.io โ€บ tutorials โ€บ add-an-item-to-the-beginning-of-an-array-in-javascript-or-node-js
Add an Item to the Beginning of an Array in JavaScript or Node.js
The Array#unshift method adds one or more items to the beginning of an array. Notice: Array#unshift returns the new length of the array, not the array containing the new item(s): const ids = [1, 2] const newArrayLength = ids.unshift(3) // 3 // the value of "ids" is [3, 1, 2] ids.unshift(4) ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/javascripttips โ€บ add new array elements at the beginning of an array in javascript
r/JavaScriptTips on Reddit: Add new array elements at the beginning of an array in JavaScript
April 19, 2023 -

Arrays are a crucial part of any programming language, and JavaScript is no exception. Arrays are an ordered collection of data that can be manipulated using various methods available in JavaScript. One of the most common operations when working with arrays is adding new elements to an array. In this article, we will discuss how to add new array elements at the beginning of an array in JavaScript.

Adding Elements to the Beginning of an Array in JavaScript

In JavaScript, the unshift()method is used to add elements to the beginning of an array. This method adds one or more elements to the beginning of an array and returns the new length of the array.

Syntax: array.unshift(element1, element2, ..., elementX)

Here, element1, element2, ..., elementX are the elements that we want to add to the beginning of the array.

Example:

const fruits = ['apple', 'banana', 'mango']; fruits.unshift('orange'); console.log(fruits); // Output: ['orange', 'apple', 'banana', 'mango'] 

In the above example, we have created an array of fruits and added a new fruit 'orange' at the beginning of the array using the unshift()method.

We can also add multiple elements to the beginning of the array using the unshift() method.

Example:

const fruits = ['apple', 'banana', 'mango']; 
fruits.unshift('orange', 'papaya', 'kiwi'); console.log(fruits); 

// Output: ['orange', 'papaya', 'kiwi', 'apple', 'banana', 'mango'] 

In this example, we have added three new fruits to the beginning of the array using the unshift() method.

Advantages of using unshift()

The unshift() method is useful in situations where we want to add elements to the beginning of an array without overwriting or removing any existing elements. It is also an efficient method as it returns the new length of the array after adding elements, making it easy to keep track of the array's length.

Conclusion

In conclusion, adding new elements to the beginning of an array in JavaScript is a straightforward process using the unshift() method. This method is efficient and returns the new length of the array after adding elements. By using this method, we can easily manipulate and modify arrays to suit our needs.

๐ŸŒ
Quora
quora.com โ€บ How-can-you-add-new-array-elements-at-the-beginning-of-an-array-in-JavaScript
How to add new array elements at the beginning of an array in JavaScript - Quora
Answer (1 of 2): You will use unshift method for inserting array element first in Javascript. Javascrpit Inserting Array element at beginning ...
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-add-new-elements-at-the-beginning-of-an-array-in-javascript.php
How to Add New Elements at the Beginning of an Array in JavaScript
<script> var fruits = ["Apple", "Banana", "Mango"]; // Prepend a single element to fruits array fruits.unshift("Orange"); console.log(fruits); // Prints: ["Orange", "Apple", "Banana", "Mango"] // Prepend multiple elements to fruits array fruits.unshift("Guava", "Papaya"); console.log(fruits); // Prints: ["Guava", "Papaya", "Orange", "Apple", "Banana", "Mango"] // Loop through fruits array and display all the values for(var i = 0; i < fruits.length; i++){ document.write("<p>" + fruits[i] + "</p>"); } </script> Please check out the tutorial on JavaScript arrays to learn about array manipulations in detail.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ add-array-beginning
JavaScript Program to Add Element to Start of an Array
JavaScript Array concat() // program ... Output ยท [4, 1, 2, 3] In the above program, the new element is added to the array variable using the unshift() method....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ Adding-an-element-at-the-start-of-the-array-in-Javascript
Adding an element at the start of the array in Javascript
Following is the example program for adding an element at the start of the array in JavaScript. <!DOCTYPE HTML> <html> <head> </head> <body> <script > const arr1 = [0,1,2,3,4,5] arr1.unshift(6) document.write(arr1) </script> </body> </html> We can add all type of data to the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-insert-elements-at-the-beginning-of-js-array
JavaScript โ€“ Insert Elements at the Beginning of JS Array | GeeksforGeeks
November 13, 2024 - Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element ...
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-insert-an-element-into-an-array-in-javascript
Push into an Array in JavaScript โ€“ How to Insert an Element into an Array in JS
November 7, 2024 - We can use the concat() method to add elements to an array without mutating or altering the original array. Instead, creating a new one is a better method if we don't want the original array to be affected.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ add-elements-to-a-javascript-array
How to Add Elements to a JavaScript Array? - GeeksforGeeks
July 23, 2025 - ... const arr = [10, 20, 30, 40]; arr.push(50); console.log("Updated Array: ", arr); arr.push(60, 70); console.log("New Updated Array: ", arr); ... The unshift() method adds one or more elements to the beginning of an array and returns the new ...