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
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ splice
Array.prototype.splice() - JavaScript | MDN
To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced(). To access part of an array without modifying it, see slice(). const months = ["Jan", "March", "April", "June"]; months.splice(1, 0, "Feb"); // Inserts at index 1 console.log(months); // Expected output: Array ["Jan", "Feb", "March", "April", "June"] months.splice(4, 1, "May"); // Replaces 1 element at index 4 console.log(months); // Expected output: Array ["Jan", "Feb", "March", "April", "May"]
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ vpqnwujy โ€บ javascript-array-insert-example
How do I insert elements into an array in JavaScript?
December 16, 2022 - You can also use the spread operator ("..."), which is the modern ES6 way of inserting elements into an array. In this JavaScript Array Insert Element example, we use the push() method to insert an element into the array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ insert-elements-in-a-js-array
JavaScript - Insert Element in an array - GeeksforGeeks
July 23, 2025 - To add an element to the end of the array, the push() method is widely used. It mutates the array and returns its updated length. ... You can insert an element at the end of an array using many other methods.
๐ŸŒ
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 - The splice() method in JavaScript arrays is used to add or remove elements from an array. You can use the splice() method to insert elements at a specific index in an array.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ push
Array.prototype.push() - JavaScript | MDN
July 12, 2026 - The push() method of Array instances adds the specified elements to the end of an array and returns the new length of the array.
Find elsewhere
๐ŸŒ
CodyHouse
codyhouse.co โ€บ blog โ€บ post โ€บ javascript-append-to-array
JavaScript quick tip - append to array with examples | CodyHouse
One important thing to remember about the push method is that it modifies the original array while returning the length of the modified array.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how to insert an item into an array at a specific index using javascript
JavaScript Insert Item into Array at Index | Sentry
3 weeks ago - Use splice() to modify in place, or slice() with spread syntax, a loop, or reduce() to insert an item without mutating the original array
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array
Array - JavaScript | MDN
July 28, 2026 - The length is memorized before the loop starts. This affects how insertions and deletions during iteration are handled (see mutating initial array in iterative methods).
๐ŸŒ
Mimo
mimo.org โ€บ tutorials โ€บ javascript โ€บ how-to-add-to-an-array-in-javascript
How to Add to an Array in JavaScript
Use splice() to insert at a specific index. Use spread (...) to create a new array without mutating. Remember that some methods modify the original array. ... Become a full-stack developer. Learn HTML, CSS, JavaScript, and React as well as NodeJS, Express, and SQL
๐ŸŒ
JavaScript in Plain English
javascript.plainenglish.io โ€บ javascript-insert-item-s-in-array-9cd818a5ae18
JavaScript โ€” Insert Item(s) In Array | by Ange IT | JavaScript in Plain English
March 28, 2024 - So `splice(1, 0, โ€˜mangoโ€™)` inserts the string โ€˜mangoโ€™ into the fruits array at index 1, without removing anything.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ insert-item-in-an-array
JavaScript Program to Insert Item in an Array | Vultr Docs
December 17, 2024 - Understand that splice() can add or remove items from an array. Choose the index where the item should be inserted. Use splice() to insert the item at the chosen index.
๐ŸŒ
Sencha
sencha.com โ€บ home โ€บ blog โ€บ how to add elements to the beginning of a javascript array: complete guide (2026)
Add Elements to Array Start in JavaScript - 2026 Guide
May 11, 2026 - Use splice() when you need to insert at a specific index. For simply adding to the beginning, unshift(), or the spread operator, is clearer. The with() method, introduced in ES2023, creates a new array with one element replaced at a given index.
๐ŸŒ
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 the Array object to add an element to a specific index, but we can use the already available splice method to achieve this.
๐ŸŒ
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 - For example, consider the following array of countries, which contains three elements (countries) arranged alphabetically: ... Suppose we want to add "Kenya," which, according to alphabetical order, should be placed in the second position, index 1 (after Ghana and before Nigeria). In that case, we will use the splice() method with the following syntax: ... The start_position specifies the index of where we want the new elements to be inserted in the array.
๐ŸŒ
CapsCode
capscode.in โ€บ blog โ€บ different-ways-to-insert-element-in-array-in-javascript
6 Ways To Insert Element In Array
November 6, 2021 - You can insert elements anywhere in the array using the splice method().
๐ŸŒ
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 - In this article we'll insert elements into an array at its beginning, end and various positions. We'll use the unshift, push, concat and slice methods!
๐ŸŒ
Medium
habtesoft.medium.com โ€บ add-elements-to-an-array-in-javascript-a9cc6cd9469f
Add elements to an array in JavaScript | by habtesoft | Medium
October 18, 2024 - One of the simplest ways to add an element to an array in JavaScript is to use the push() method.