Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
3 of 3
Active reading.
Peter Mortensen

Another possible solution, with usage of Array.reduce.

const arr = ["apple", "orange", "raspberry"];
const arr2 = [1, 2, 4];

const insert = (arr, item, index) =>
  arr.reduce(function(s, a, i) {
    i === index ? s.push(item, a) : s.push(a);
    return s;
  }, []);

console.log(insert(arr, "banana", 1));
console.log(insert(arr2, 3, 2))

kind user