How to get N number of elements from an array in JavaScript?

I am trying to build an application where I want to display data from the javascript array. There will be a lot of data and I want to show pagination and 10 items per page. Suppose the user wants to see the 3rd page, then I need to get the 20th to 29th item from the array.

let listItems = [item1, item2, item3, item4, ...., item100];

Can anyone help me solve the problem?

You can use slice to get ranged elements. You can set starting and ending index and get all data between those index.

array.slice(start, end);

You need to calculate the start and end index from user selected page number.

let listItems   = [item1, item2, item3, item4, ...., item100];
let perPage     = 10;
let pageNumber  = 3;
let endIndex    = (perPage * pageNumber) - 1;
let startIndex  = endIndex - perPage;
let newArr      = listItems.slice(startIndex, endIndex);

console.log(newArr);
// new array with selected items