How to paginate JavaScript array?

To paginate JavaScript array, we use the slice method.

For instance, we write

const paginate = (array, pageSize, pageNumber) => {
  return array.slice((pageNumber - 1) * pageSize, pageNumber * pageSize);
};

console.log(paginate([1, 2, 3, 4, 5, 6], 2, 2));

to define the pagination function that calls array.slice with the start and end index of the chunk of the array to return.

The item at the end index itself is not included with the returned array.