Why does splice return an empty array if in console.log() or the return statement?

I have been trying to figure something out. I did a bit of research after struggling through the Slice and Splice basic algorithm and scripting challenge because of something seemingly simple.

Why does splice return an empty array if in console.log() or the return statement?
ex.

a1 = [1, 2, 3];
a2 = [4, 5, 6];

console.log(a1.splice(1, 0, ...a2));

// returns []

where as running splice outside of console.log() or return statement returns the desired result.

a1 = [1, 2, 3];
a2 = [4, 5, 6];
a1.splice(1, 0, ...a2)

console.log(a1);

// returns [ 1, 4, 5, 6, 2, 3 ]

.splice modifies the target array and returns an array of the removed elements. In the first snippet you’re passing the array of removed elements directly to console.log (an empty array is printed since no elements are removed from a1). In the second snippet you’re printing the then modified value of a1 after the splice, which contains the combined elements from both arrays.

1 Like

Ah, I got it. Thanks!