How to Insert an Item into an Array at a Specific Index in JavaScript

Answer: Use the JavaScript splice() Method

To insert a value, or item into an array at a particular index in JavaScript, you can use the  splice()  function. This is an effective and versatile way to perform array manipulations.

The  splice() method has the syntax like  array.splice(startIndexdeleteCount, item1, item2,...)This method allows you to add elements to an array by setting the  deleteCount to 0,  You can also specify at least one additional element as shown in the following example.

    var persons = ["Harry", "Clark", "John"];
    // Insert an item at 1st index position
    persons.splice(1, 0, "Alice");
    console.log(persons); // Prints: ["Harry", "Alice", "Clark", "John"]
    // Insert multiple elements at 3rd index position
    persons.splice(3, 0, "Ethan", "Peter");
    console.log(persons); // Prints: ["Harry", "Alice", "Clark", "Ethan", "Peter", "John"]

 push() and unshift() are both options that can be used to add items to an array’s beginning or end. To learn more about array manipulation, see the JavaScript Arrays tutorial.