Skip to content

Pagination Boundary Mistake

Quick summary

Your slice start or end is off by one page size — the first or last item of a page is duplicated or dropped.

Why this happens

  • Using (page - 1) * size vs page * size inconsistently

  • Forgetting that slice end is exclusive while start is inclusive

  • Off-by-one when page numbers start at 1 but indices start at 0

Minimal example

✗ Broken code
const items = [1,2,3,4,5,6,7,8,9,10];
const page = 2, size = 3;
const start = page * size; // 6 — skips items 4,5,6
const view = items.slice(start, start + size);
console.log(view); // [7,8,9]
✓ Fixed code
const items = [1,2,3,4,5,6,7,8,9,10];
const page = 2, size = 3;
const start = (page - 1) * size; // 3 — correct
const view = items.slice(start, start + size);
console.log(view); // [4,5,6]

Using page * size for page 2 gives start 6, skipping items 4-6. Using (page - 1) * size gives start 3, which is the correct beginning of the second page.

How to diagnose

  • Print the start and end indices for pages 1, 2, 3 and check for gaps or overlaps

  • Verify the total items shown across all pages equals the source length

  • Test with a small array where you can list every page's contents

How to fix

  • Use start = (page - 1) * size and end = start + size consistently

  • Remember slice end is exclusive — do not subtract 1 from end

  • Clamp end to the array length to avoid an empty last page

How to prevent

  • Write a small pagination test that checks no overlaps and full coverage

  • Use a helper function so the formula is defined in one place

Related Resources

Related Glossary

Related Lessons

  • Arrays

    Learn about slicing and pagination.

← Back to language