How can you divide an array in 2 parts, divided exactly in the middle?

Use the Array instance splice() method:

const list = [1, 2, 3, 4, 5, 6]
const half = Math.ceil(list.length / 2);    

const firstHalf = list.splice(0, half)
const secondHalf = list.splice(-half)

If the list contains an even number of items, the result is split with exactly half the items.

If the number is odd, for example

[1, 2, 3, 4, 5]

The result will be

[ 1, 2, 3 ]
[ 4, 5 ]

Download my free JavaScript Beginner's Handbook


HUGE PROMO LIVE NOW
Black Friday!