You want the splice function on the native array object.

arr.splice(index, 0, item); will insert item into arr at the specified index (deleting 0 items first, that is, it's just an insert).

In this example we will create an array and add an element to it into index 2:

var arr = [];
arr[0] = "Jani";
arr[1] = "Hege";
arr[2] = "Stale";
arr[3] = "Kai Jim";
arr[4] = "Borge";

console.log(arr.join()); // Jani,Hege,Stale,Kai Jim,Borge
arr.splice(2, 0, "Lene");
console.log(arr.join()); // Jani,Hege,Lene,Stale,Kai Jim,Borge

UPDATE (24 May 2024)

You can now use the toSpliced method which behaves just like splice, however it returns a new array without mutating the existing one.

You could update the previous example like so:

const updated = arr.toSpliced(2, 0, "Lene");
Answer from tvanfosson on Stack Overflow
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript array insert at 0 | example code
JavaScript array insert at 0 | Example code
January 25, 2023 - Use Array unshift method to insert an element at 0 indexes into the JavaScript array. The unshift. It's like push, except it adds elements..
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › unshift
Array.prototype.unshift() - JavaScript | MDN
const array = [1, 2, 3]; ... to add to the front of the arr. The new length property of the object upon which the method was called. The unshift() method inserts the given values to the beginning of an array-like object....
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 ...
🌐
DigitalOcean
digitalocean.com › community › questions › how-to-insert-an-item-into-an-array-at-a-specific-index-with-javascript
How to insert an item into an array at a specific index with JavaScript? | DigitalOcean
April 9, 2023 - To insert an item at a specific index, you can use the splice() method with the following syntax: ... Here, index is the position where you want to insert the item, 0 indicates that no elements should be removed, and item is the element you ...
🌐
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 - Suppose you have an array of numbers: ... And you want to insert the number 3 at index 2 (remember that JavaScript uses zero-based indexing). Here's how you can accomplish this: const index = 2; const newNumbers = [ ...numbers.slice(0, index), 3, ...numbers.slice(index) ]; console.log(newNumbers); // Output: [1, 2, 3, 4, 5]
🌐
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....
🌐
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 - Sencha Ext JS provides built-in store management with array prepending capabilities: // Creating a store with initial data const store = Ext.create('Ext.data.Store', { fields: ['id', 'name', 'priority'], data: [ { id: 2, name: 'Review code', priority: 'medium' }, { id: 3, name: 'Write tests', priority: 'low' } ] }); // Prepending a record using insert at index 0 store.insert(0, { id: 1, name: 'Critical bug fix', priority: 'high' }); // For grid panels, the UI updates automatically const grid = Ext.create('Ext.grid.Panel', { store: store, columns: [ { text: 'ID', dataIndex: 'id' }, { text: 'Task', dataIndex: 'name', flex: 1 }, { text: 'Priority', dataIndex: 'priority' } ], renderTo: Ext.getBody() }); // Prepending triggers grid refresh store.insert(0, { id: 0, name: 'Emergency task', priority: 'critical' });
Find elsewhere
🌐
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
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"
🌐
ReqBin
reqbin.com › code › javascript › vpqnwujy › javascript-array-insert-example
How do I insert elements into an array in JavaScript?
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 ...
🌐
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 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. The following parameter(s) or element(s) may be more than one, as these are the elements we want to add ...
🌐
Scaler
scaler.com › home › topics › javascript program to insert item in an array
JavaScript Program to Insert Item in an Array - Scaler Topics
May 4, 2023 - As we already noted in the beginning that the array in JS is 0 indexed base, that’s why the last item in the array is at the index=array_length-1 and when we add a new item at the end of the array, then it should be at the index=array_length.
🌐
W3docs
w3docs.com › javascript
How to Insert an Item into an Array at a Specific Index
The array.splice() array method is used to add or remove items from array taking three arguments: the index where the element id should be inserted or removed, the number of items that should be deleted, and the new items that should be inserted. The insertion will be setting the number of elements to be deleted to 0...
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]

🌐
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 - ... let a = [10, 20, 30, 40]; // ... 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....
🌐
Programiz
programiz.com › javascript › examples › insert-item-array
JavaScript Program to Insert Item in an Array
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to insert an item at a specific index into an array function insertElement() { let array = [1, 2, 3, 4, 5]; // index to add to let index = 3; // element that you want to add let element = 8; array.splice(index, 0, element); console.log(array); } insertElement();
🌐
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
🌐
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