How to insert an array inside another array with JavaScript?

To insert an array inside another array with JavaScript, we use the splice method.

For instance, we write

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

to call splice with the entries in the a2 array spread as arguments of splice to insert the entries at index 2 of a1.

Then entries in a1 after index 2 are moved to the end of the array.