To splice an array in half no matter the size with JavaScript, we use the slice
method.
For instance, we write
const halfLength = Math.ceil(arrayName.length / 2);
const leftSide = arrayName.slice(0, halfLength);
to call Math.ceil
with arrayName.length / 2
to get the index of the middle element.
Then we call slice
with 0 and halfLength
to return the first half of the arrayName
array.