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

Here's a simple function that supports inserting multiple values at the same time:

function add_items_to_array_at_position(array, index, new_items)
{
    return [...array.slice(0, index), ...new_items, ...array.slice(index)];
}

Usage example:

let old_array = [1,2,5];

let new_array = add_items_to_array_at_position(old_array, 2, [3,4]);

console.log(new_array);

//Output: [1,2,3,4,5]
Pikamander2