Lesson 28 of 28

In progress

Course progress

JavaScript

Capstone: a to-do app that persists

Build a to-do app with add, toggle, delete, filters and saved state, then review it for keyboard access and failure cases.

This is the last lesson in the track. It combines state, rendering, events, storage and error handling in one small application, and it is the shape most front-end work follows.

What you will learn

  • Model a small app as state plus a render function.
  • Implement add, toggle, delete and filters without losing data.
  • Persist to localStorage and handle corrupted storage.

The data model

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Two fields: the list and the current filter. Everything else — counts, which rows to draw, whether the empty state shows — is derived. Derived values cannot go stale.

Because removeTask reassigns state.tasks rather than mutating the array, every part of the app reads the list through state instead of keeping a copy. A stale copy is the most common source of "deleted task came back" bugs.

Ids should be unique and stable. crypto.randomUUID() is available in browsers; for a simple app, a counter based on the highest existing id also works and survives storage.

Rendering

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Rebuilding the list from state keeps the DOM honest. The checkbox carries the id, and the delete button carries a different data attribute, so one delegated handler can tell them apart.

Events and state changes

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Each function changes state only. addTask returns whether it added, so the caller can clear the input or show a message. That small contract keeps the UI logic out of the data logic.

A single delegated listener wires them together:

list.addEventListener("change", (event) => {
if (event.target.matches("[data-id]")) {
toggle(Number(event.target.dataset.id));
render(state, list);
}
});
list.addEventListener("click", (event) => {
const button = event.target.closest("[data-delete]");
if (!button) return;
removeTask(Number(button.dataset.delete));
render(state, list);
});

change is right for checkboxes; click is right for buttons. Delegation means the handlers survive every re-render.

Filters and counts

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Keep the active filter in the URL hash if a refresh should preserve it; otherwise the default of all is the least surprising choice. Filter buttons should have aria-pressed reflecting the active one, and the list should be labelled. The count belongs in a role="status" element so it is announced when it changes.

The empty state needs to distinguish "no tasks yet" from "none match this filter". Those are different messages, and a generic "Nothing here" makes the filter look broken.

Persistence

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

Saving after every change means a closed tab loses nothing. The array check protects against valid JSON with the wrong shape, and the catch keeps a corrupted value from breaking the app on load.

Review checklist

  1. Add a task with only spaces: nothing is added, and the input stays usable.
  2. Toggle a task, reload, and confirm the state survived.
  3. Delete the last task: the empty state appears with the right message.
  4. Switch filters with the keyboard: aria-pressed follows the active filter.
  5. Tab through the list: every checkbox and delete button is reachable and labelled.
  6. Paste a very long title: the row wraps instead of forcing the page sideways.

Storing the filtered list instead of the full list means deleting while a filter is active can erase tasks you cannot see. Persist the complete list, and apply the filter only when rendering.

Add an "edit title" action: a button that replaces the row’s text with an input, saves on Enter or blur, and cancels on Escape. Keep state as the source of truth.

Show solution
JavaScript
Editable. Runs in your browser.





Press Run to see the output.

In the interface, replace the label with an input whose value is the current title, focus it, and select the text. On Enter or blur, call rename and re-render; on Escape, re-render without changing state. Because the DOM is rebuilt from state, cancelling is just a render.

Where to go next

You have finished JavaScript. Useful next steps: build a small app that calls a real API, add a browser test runner, and learn a framework now that you know what it abstracts. The concepts — state, render, events, derived values — transfer directly.

Recap

  • State first, render from state, and derive counts rather than storing them.
  • Change state in small functions, then re-render; delegate events from stable parents.
  • Persist the full list, guard corrupted storage, and check keyboard access before shipping.

Why rebuild the whole list on every change?
It is simple and correct at this size. For hundreds of rows, update only the changed row after measuring the cost.
Should tasks have timestamps?
Add `createdAt` as an ISO string if you want ordering or “added today” labels. Strings sort correctly and survive JSON.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.