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.
๐ŸŒ
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
[val, currentValue] : currentValue), [], ); } console.log(insert(arr, value, index)); // "walk the dog", "go shopping", "go to the pharmacy", "exercise" This method creates a new array; it does not change the original array. YoutubeHow Sentry.io saved me from disaster (opens in a new tab) ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns (opens in a new tab)
๐ŸŒ
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 - Discover the versatility of methods ... from an array. Choose the index where the item should be inserted. Use splice() to insert the item at the chosen index....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ insert-elements-in-a-js-array
JavaScript - Insert Element in an array - GeeksforGeeks
July 23, 2025 - In JavaScript elements can be inserted at the beginning, end, and at any specific index. JS provides several methods to perform the operations. This operation inserts an element at the start of the array.
๐ŸŒ
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
<script> var persons = ["Harry", ... To add items at the end or beginning of an array you can simply use the push() and unshift() array methods....
Find elsewhere
๐ŸŒ
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 - 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.
๐ŸŒ
W3docs
w3docs.com โ€บ javascript
How to Insert an Item into an Array at a Specific Index
function insertAt(array, index, ...elementsArray) { array.splice(index, 0, ...elements); } 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] ...
๐ŸŒ
Fjolt
fjolt.com โ€บ article โ€บ javascript-how-to-insert-at-index-array
Inserting an Item into an Array at a Specific Index in Javascript
Adds 'is', and 'cool' at index 2. As you can see, you can add an infinite number of new items to your array using this method. It manpulates an array - so the original array is changed by this function. You donโ€™t have to delete any items using splice, but you can if you want. Either leaving the delete count as 0, or empty, will mean no items are deleted from your array. Any data inserted in the third argument (or any argument after the third argument) is added to the array at the specified index.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ insert-item-array
JavaScript Program to Insert Item in an Array
The first argument specifies the index where you want to insert an item. The second argument (here 0) specifies the number of items to remove. The third argument specifies the element that you want to add to an array. // program to insert an item at a specific index into an array function ...
๐ŸŒ
Ubiq BI
ubiq.co โ€บ home โ€บ how to insert item into js array at specific index
How to Insert Item Into JS Array At Specific Index - Ubiq BI
May 16, 2025 - Spread operator is a versatile operator in JavaScript that allows you to easily concatenate arrays. It is also used to easily copy part of array or object into another array or object. It is also used for deconstructing arrays but that is not relevant here. It is supported by all ES6 web browsers and can be easily used to insert one or more items into a JS array at specific index.
๐ŸŒ
Quora
quora.com โ€บ In-Javascript-whats-the-best-way-to-insert-an-element-into-an-array-at-a-specific-index
In Javascript, what's the best way to insert an element into an array at a specific index? - Quora
Answer (1 of 20): Best can be rather subjective and even in the simplest of cases the term is very much influenced by the constraints of what youโ€™re building. Iโ€™ll preface this by saying [code ]Array.prototype.splice[/code] is a perfectly acceptable option.
๐ŸŒ
DEV Community
dev.to โ€บ smpnjn โ€บ inserting-an-item-into-an-array-at-a-specific-index-in-javascript-3g1c
Inserting an Item into an Array at a Specific Index in Javascript - DEV Community
September 18, 2022 - Adds 'is', and 'cool' at index 2. As you can see, you can add an infinite number of new items to your array using this method. It manpulates an array - so the original array is changed by this function. You don't have to delete any items using splice, but you can if you want. Either leaving the delete count as 0, or empty, will mean no items are deleted from your array. Any data inserted in the third argument (or any argument after the third argument) is added to the array at the specified index.
๐ŸŒ
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 - To insert element into JavaScript array, splice method should be used. For inserting first parameter should be index before which new element will be inserted. Indexes are counted from zero.
๐ŸŒ
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 - var num = [1, 2, 3, 6, 7, 8]; // insertAt() arguments // 1. source array - num // 2. index to insert - 3 // 3.