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
๐ŸŒ
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 is called on the numbers array, starting at index 2, with a deleteCount 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 index.
๐ŸŒ
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 - In this example, we inserted the โ€˜grapeโ€™ element at index 1 in the fruits array. The splice() method offers a simple and efficient way to insert an item into an array at a specific index in JavaScript.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-insert-an-item-into-an-array-at-a-specific-index-in-javascript.php
How to Insert an Item into an Array at a Specific Index in JavaScript
You can use the splice() method to insert a value or an item into an array at a specific index in JavaScript. This is a very powerful and versatile method for performing array manipulations.
๐ŸŒ
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 - To add an element to the end of an array, we can use the fact that the length of an array is always one less than the index. Say, the length of an array is 5, then the last index at which the value will be 4. So, we can directly add the element at the last+1 index.
๐ŸŒ
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
The first argument of the splice() ... The second argument is the deleteCount. To add elements to an array, add each element as an additional argument....
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ insert-item-array
JavaScript Program to Insert Item in an Array
// program to insert an item at a specific index into an array function insertElement() { let array = [1, 2, 3, 4]; // index to add to let index = 3; // element that you want to add let element = 8; for (let i = array.length; i > index; i--) { //shift the elements that are greater than index ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-inserting-multiple-items-at-specific-index-in-js-array
JavaScript - Inserting Multiple Items at Specific Index in JS Array ...
July 11, 2025 - ... let a = [10, 20, 30, 40, 50]; const i = 2; const a1 = [99, 100, 101]; a = [...a.slice(0, i), ...a1, ...a.slice(i)]; console.log(a); ... JS Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ insert-elements-in-a-js-array
JavaScript - Insert Element in an array - GeeksforGeeks
July 23, 2025 - Refer to this article for more methods. To insert an element at a specific index, the splice() method can be used. This method allows adding elements at any position in the array while optionally removing existing ones.
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Insert an Item into an Array at a Specific Index
In the example, we create an array and add an element to it into index 2: ... let arr = []; arr[0] = "Javascript"; arr[1] = "Python"; arr[2] = "C#"; arr[3] = "Java"; console.log(arr.join()); arr.splice(2, 0, "C++"); console.log(arr.join());
๐ŸŒ
Plain English
plainenglish.io โ€บ blog โ€บ insert-an-element-in-specific-index-in-javascript-array
Insert an Element in Specific Index in JavaScript Array
November 11, 2019 - We have some in-built methods to ... โ†’ Add an element to the beginning of an array. To add an element to the specific index there is no method available in Array object....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript-inserting-multiple-items-at-specific-index-in-js-array
JavaScript โ€“ Inserting Multiple Items at Specific Index in JS Array | GeeksforGeeks
November 15, 2024 - We can insert an item into Array at a specific index using the JavaScript Array splice method. This method removes the elements from the mentioned index and add items at that specific position.
๐ŸŒ
Maslosoft
maslosoft.com โ€บ knowledge base โ€บ how to add array element at specific index in javascript?
How to add array element at specific index in JavaScript?
September 10, 2020 - Passing as parameter on integer value greater than zero, results in adding element before that index value. var x = [111, 222, 333]; x.splice(2, 0, 666); // Array is now [111, 222, 666, 333] If we set index parameter to be greater than maximum existing array index or negative value, the new element will be appended to end of array. However good practice is to avoid such constructs, instead use array.length or better push method.
๐ŸŒ
Fjolt
fjolt.com โ€บ article โ€บ javascript-how-to-insert-at-index-array
Inserting an Item into an Array at a Specific Index in Javascript
Any data inserted in the third argument (or any argument after the third argument) is added to the array at the specified index. Here is another example, where we insert โ€˜broccoliโ€™ into an array at index 2: let arr1 = [ 'potato', 'banana', 'ravioli', 'carrot' ]; // Inserts broccoli at position 2, after deleting 0 items arr1.splice(2, 0, 'broccoli'); // Returns [ 'potato', 'banana', 'ravioli', 'brccoli', 'carrot' ] console.log(arr1); ... Javascript loops: for vs forEach vs for..
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ splice
Array.prototype.splice() - JavaScript | MDN
Zero-based index at which to start changing the array, converted to an integer. Negative index counts back from the end of the array โ€” if -array.length <= start < 0, start + array.length is used. If start < -array.length, 0 is used. If start >= array.length, no element will be deleted, but the method will behave as an adding function, adding as many elements as provided.
๐ŸŒ
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 - This adds 'yellow' and 'pink' starting at index 2. This action demonstrates splice()โ€™s capability to insert multiple items at once. Recognize that push() adds items to the end of an array.
๐ŸŒ
David Walsh
davidwalsh.name โ€บ array-insert-index
Array: Insert an Item at a Specific Index with JavaScript
July 25, 2014 - Array.prototype.insert = function (index, item) { this.splice(index, 0, item); }; I've tinkered around quite a bit with arrays, as you may have noticed: ... Arrays are super useful -- JavaScript just makes some tasks a bit more ... code-heavy ...
๐ŸŒ
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 - In this example, we have an array of colors and we use the splice() method to add a single element and multiple elements at a specified index. ... The concat() method can also be used to add elements to an array in JavaScript.
๐ŸŒ
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 - The start_position specifies the index of where we want the new elements to be inserted in the array. If there are multiple elements, it specifies where the elements inserted will start.