How can the following operation be done without mutating the array:
let array = [\'item1\']; console.log(array); // [\'item1\'] array[2] = \'item2\'; // array is
Another way could be to use spread operator with slice as
let newVal = 33, position = 3; let arr = [1,2,3,4,5]; let newArr = [...arr.slice(0,position - 1), newVal, ...arr.slice(position)]; console.log(newArr); //logs [1, 2, 33, 4, 5] console.log(arr); //logs [1, 2, 3, 4, 5]