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
4059

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]

🌐
W3Schools
w3schools.com › jsref › jsref_unshift.asp
JavaScript Array unshift() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
People also ask

What happens internally when JavaScript shifts array elements?
Conceptually, the engine ensures capacity for the new size, moves existing elements up by one (or by the number of inserted items), inserts the new element(s) at index 0, and updates the length. The shifting work is why prepending scales with array size.
🌐
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 ...
Which method should you use in 2026: unshift(), spread, or concat()?
Use unshift() when mutating the same array is acceptable and the array isn’t huge. Use spread ([x, ...arr]) when you need an immutable update and the array size is reasonable. Use concat() when you want immutability but need a safer approach for larger arrays or to avoid heavy spreads.
🌐
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 ...
Why does adding items at the start of an array feel “slow” sometimes?
Because JavaScript arrays are index-based. When you insert at index 0, existing elements usually shift to new indices (1, 2, 3, …). That shifting grows with array size and becomes noticeable on large arrays or frequent prepends—especially in UI-heavy apps.
🌐
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 - Because JavaScript arrays are index-based. When you insert at index 0, everything else usually needs to shift to 1, 2, 3….
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › unshift
Array.prototype.unshift() - JavaScript | MDN
const arr = [1, 2]; arr.unshift(0); ... arr.unshift([-7, -6], [-5]); // the new array length is 8 // arr is [ [-7, -6], [-5], [-4, -3], -2, -1, 0, 1, 2 ] The unshift() method reads the length property of this....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › push
Array.prototype.push() - JavaScript | MDN
Instead, we store the collection on the object itself and use call on Array.prototype.push to trick the method into thinking we are dealing with an array—and it just works, thanks to the way JavaScript allows us to establish the execution context in any way we want. ... const obj = { length: 0, addElem(elem) { // obj.length is automatically incremented // every time an element is added.
🌐
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 - This method is similar to the push() method but it adds an element at the beginning of the array. ... The array.splice method is used to modify the content of the array. we give the start, end the value as a parameter to the method so that it helps us to add the value in between the given start and end index. ... The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. ... The JavaScript Array concat() Method is used to merge two or more arrays together.
🌐
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 - If there are multiple elements, it specifies where the elements inserted will start. If we want to add to the array, we set the second argument to zero (0), instructing the splice() method not to delete any array elements.
Find elsewhere
🌐
Educative
educative.io › answers › how-to-insert-an-element-in-a-specific-index-in-javascript-array
How to insert an element in a specific index in JavaScript Array
There is no method available in ... this. An array starts from index 0, so if we want to add an element as the first element of the array, then the index of the element is 0....
🌐
ReqBin
reqbin.com › code › javascript › vpqnwujy › javascript-array-insert-example
How do I insert elements into an array in JavaScript?
December 16, 2022 - If deleteCount is 0, then the elements are not deleted. If deleteCount is more significant than the number of remaining elements in the array, then all remaining elements of the array will be deleted · element1, ..., elementX (optional): one ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
2 weeks ago - testSideEffect((arr, index) => { if (index > 0) arr[index - 1] += "*"; }); // array: [e1, e2, e3, e4], index: 0, elem: e1 // array: [e1, e2, e3, e4], index: 1, elem: e2 // array: [e1*, e2, e3, e4], index: 2, elem: e3 // array: [e1*, e2*, e3, e4], index: 3, elem: e4 // Final array: [e1*, e2*, e3*, e4] Inserting n elements at unvisited indexes that are less than the initial array length will make them be visited.
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
The first parameter (0) defines the position where new elements should be added (spliced in). The second parameter (1) defines how many elements should be removed. The rest of the parameters are omitted.
🌐
Sentry
sentry.io › sentry answers › javascript › how to insert an item into an array at a specific index using javascript
How to insert an item into an array at a specific index using JavaScript | Sentry
For example, inserting an item at a specific index in a to-do list. There are various methods to insert an item into an array, broadly divided into those that change the original array and those that don’t. The splice() method is used to change an array by adding, removing, or replacing elements. This method modifies the original array. ... const arr = ["walk the dog", "go shopping", "exercise"]; const index = 2; const value = "go to the pharmacy"; arr.splice(index, 0, value); console.log(arr); // "walk the dog", "go shopping", "go to the pharmacy", "exercise"
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-insert-elements-at-the-beginning-of-js-array
JavaScript - Insert Elements at the Beginning of JS Array - GeeksforGeeks
July 23, 2025 - JavaScript · let a = [10, 20, ... the elements at the beginning of an array, shift the elements of the array to the right by 1 index and insert the new element at the 0th index....
🌐
freeCodeCamp
freecodecamp.org › news › insert-into-javascript-array-at-specific-index
How to Insert into a JavaScript Array at a Specific Index – JS Push
November 7, 2024 - In this code, the splice() method ... of 0. You then add the new element 3 to the array at the start index. The result is the modified array [1, 2, 3, 4, 5]. In this article, you have learned the two major techniques for inserting elements into a JavaScript array at a specific ...
🌐
W3Schools
w3schools.com › jsref › jsref_push.asp
JavaScript Array push() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
🌐
HostingAdvice
hostingadvice.com › home › how-to › javascript "add to array" functions (push vs unshift vs others)
JavaScript "Add to Array" Functions (push vs unshift vs others)
March 25, 2023 - If you want to insert an element (or elements) into a particular point somewhere within the array, besides the beginning or end, then you should most likely be using the splice() method. To use splice() your code should look like this: var list = ["foo", "bar"]; list.splice( 1, 0, "baz"); // at index position 1, remove 0 elements, then add "baz" to that position // element "bar" will now automatically be moved to index position 2 ["foo", "baz", "bar"] // result ·
🌐
CapsCode
capscode.in › blog › different-ways-to-insert-element-in-array-in-javascript
6 Ways To Insert Element In Array
November 6, 2021 - The first parameter you define where in the array you want to insert data. The second parameter you define how many elements you want to delete. We only want to insert elements, so we put 0 here.
🌐
Stack Abuse
stackabuse.com › javascript-how-to-insert-elements-into-a-specific-index-of-an-array
JavaScript: How to Insert Elements into a Specific Index of an Array
September 25, 2023 - The array is 3 in length, and the 2nd element is 3. There is no 3rd element, so we're greeted with undefined. Finally, at that position, we insert the value of 4.