Slice and Splice Clarifications

Hi all - I had the right code except for the spread operator in the splice call (…arr1) and I was trying a dozen different ways to include the first array values only and not as a subarray. Why does the spread operator make that happen?

function frankenSplice(arr1, arr2, n) {
let newArr = arr2.slice();
newArr.splice(n, 0, arr1);
return newArr;


console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Slice and Splice

Link to the challenge:

It’s the spread operator paired with splice ability to accept multiple items to add. Spread operator expands arr1 and allows splice do all the job just in one go.

It kind of can be looked at as an equivalent of loop below, inserting elements one by one and adjusting the start index.

  for (let i = 0; i < arr1.length; i++) {
    newArr.splice(n + i, 0, arr1[i]);
1 Like