Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
2 of 6
added 154 characters in body
Luis Perez

If you want to insert multiple elements into an array at once check out this Stack Overflow answer: A better way to splice an array into an array in javascript

Also here are some functions to illustrate both examples:

function insertAt(array, index) {
    var arrayToInsert = Array.prototype.splice.apply(arguments, [2]);
    Array.prototype.splice.apply(array, [index, 0].concat(arrayToInsert));
}

function insertArrayAt(array, index, arrayToInsert) {
    Array.prototype.splice.apply(array, [index, 0].concat(arrayToInsert));
}

Finally here is a jsFiddle so you can see it for youself: http://jsfiddle.net/luisperezphd/Wc8aS/

And this is how you use the functions: insertAt(arr, 1, "x", "y", "z"); var arrToInsert = ["x", "y", "z"]; insertArrayAt(arr, 1, arrToInsert);

Luis Perez