Learning objective: Implement search, filters, sorting and pagination as pure functions over state, and render the visible page with counts and accessible controls.
Pagination shows one slice at a time: page n of size s is list.slice((n - 1) s, n s). The number of pages is Math.ceil(total / s).
Keep the query, filters, sort and page number in state, and compute the visible items from the full list every time.
Apply steps in a fixed order: search → filter → sort → paginate. Always reset to page 1 when the query or filters change, or people land on an empty page.
Clamp the page number between 1 and the page count, and handle zero results with a helpful empty state.
Sort a copy ([...list].sort(...) or toSorted) so the source data is never reordered, and use localeCompare for text.
Announce result counts in a live region ("12 results"), mark the current page with aria-current="page", and keep state in the URL (?q=lamp&page=2) so results can be shared and survive reloads.
05Definitions
Search
Keeping only items whose text matches a query.
Filter
Keeping only items that match chosen criteria, such as a category.
Pagination
Splitting a long list into pages and showing one page at a time.
Page count
The number of pages, Math.ceil(total / pageSize).
aria-current
An attribute marking the current item in a set, such as the current page link.
Search and filter interface. Type a query, pick filters and change pages on a sample catalogue, and see each step of the pipeline: all, searched, filtered, sorted, paged.
const names = ["Ada", "Alan", "Grace", "Linus", "Margaret", "Barbara", "Ken", "Dennis", "Anita", "Radia"];
// state = { q: "", page: 1 }, page size 3.
// render(): search names (case-insensitive), clamp page, show the page in
// #list, #status "Page P of N (T results)", disable Prev/Next at the ends.
// Typing in #q sets q and resets page to 1. Prev/Next change page.
// Click Next twice, then type "a" and click Next once; log #status.
09Prediction
Commit to an answer first, then run the real code to compare.
const n = 23, size = 10;
console.log(Math.ceil(n / size), Math.floor(n / size), n % size);
What is printed?
10Debugging investigation
Search shows "no results" although there are matches
On page 3 of all products, searching for "lamp" shows an empty list, although two lamps exist.
An online store keeps the search box, category filter and page number in the URL (?q=lamp&cat=lighting&page=2). Reloading or sharing the link shows exactly the same results, because the page rebuilds its state from the URL and runs the same search, filter and pagination functions.
const params = new URLSearchParams("q=lamp&cat=lighting&page=2");
console.log(params.get("q"), params.get("cat"), Number(params.get("page")));
12Knowledge check
1. Which expression gives page n (from 1) of size s?
list.slice(n s, n s + s)
list.slice((n - 1) s, n s)
list.slice(n, s)
list.splice((n - 1) * s, s)
2. How many pages do 21 items need at 5 per page?
4
5
4.2
6
3. What should happen to the page number when the search query changes?
Stay the same
Reset to 1
Go to the last page
Become 0
4. Why is the original product list reordered after sorting results?
const results = products.sort((a, b) => a.price - b.price);
sort sorts the array in place
filter reorders
slice changes the original
map sorts
5. In what order should the steps run?
page → sort → filter → search
search → filter → sort → page
sort → page → search
Any order
6. How should the result count be shown?
Only in the page title
In a status live region near the results
In the console
Not at all
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Page 1 skips the first items: Prints a,b; Last page is partial
Filter and sort books: Prints both queries; Does not reorder the source; No matches gives []
A searchable, paged list: Prints the status; No results
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-15-interactive-application-architecture/lesson-074-filtering-searching-and-pagination/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Search, filter, sort and paginate with small pure functions.
Run them in order: search → filter → sort → page.
Reset to page 1 when the query changes and clamp the page.
Show counts and empty states, and mark the current page accessibly.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationfiltering, searching and pagination
Context: I am learning filtering, searching and pagination in JavaScript.
Task: Explain a search, filter, sort and pagination pipeline with a product list.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionfiltering, searching and pagination drill
Context: I have practised filtering, searching and pagination.
Task: Give me six pipeline questions with lists, queries and page sizes, and ask what is shown.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingfiltering, searching and pagination bug
Context: After searching, my list is empty although there are matches.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationfiltering, searching and pagination practice
Context: I finished the filtering, searching and pagination exercises.
Task: Create three exercises that build search, filters and pagination with exact expected output.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewfiltering, searching and pagination review
Context: Here is my code that uses filtering, searching and pagination.
Task: Review my list code for in-place sorting, missing page resets and empty states.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.