Just a few weeks until the 2021 JavaScript Full-Stack Bootcamp opens.
Signup to the waiting list!


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 ]


The 2021 JavaScript Full-Stack Bootcamp will start at the end of March 2021. Don't miss this opportunity, signup to the waiting list!

More js tutorials: