Projects

Todo App

Build a fully functional Todo app — the classic starter project. This project teaches state management, DOM rendering, CRUD operations, event handling, filtering/sorting, and localStorage persistence.

What You Will Build

1

State & CRUD

Add, edit, toggle, delete todos with immutable state updates

2

Filter & Sort

All / Active / Done / High Priority, sort by date/priority/alpha

3

Persistence

Save to localStorage, load on startup, handle storage errors

4

Stats & UX

Completion rate, counts by priority, keyboard shortcuts

Core Concepts Demonstrated

Immutable State Updates

Always create new arrays — todos = todos.filter(...) — never mutate in place. Makes debugging easier.

Derived State

Active count, completion rate, filtered list — computed from source array, not stored separately.

Event Delegation

One listener on <ul> handles all item clicks. No re-binding listeners after re-renders.

Optimistic UI

Update UI immediately, persist to storage after. Feels instant even on slow devices.

Input Validation

Trim whitespace, reject empty strings, limit length. Always validate before adding to state.

Error Isolation

localStorage.setItem can throw (QuotaExceededError). Wrap in try/catch, degrade gracefully.

Common Pitfalls

Using array index as todo ID — IDs shift when items are deleted, causing toggle/delete bugs
Use an auto-incrementing counter (nextId++) or crypto.randomUUID() for stable, unique IDs.
Mutating state directly: todos[0].done = true — breaks change detection and immutability
Use map: todos = todos.map(t => t.id === id ? {...t, done: true} : t). Create a new array.
Re-binding event listeners on every render — memory leak from duplicate handlers
Use event delegation: one listener on the parent container, check event.target to determine action.
Storing derived state (filteredTodos, count) separately — gets out of sync
Compute derived values from the source array on each render. Store only the minimal source of truth.

Build It Live

Code Editor

<form id="form"><input id="text" placeholder="What needs doing?"><button>Add</button></form>
<div id="filters"><button data-f="all">All</button><button data-f="open">Open</button><button data-f="done">Done</button></div>
<ul id="list"></ul>
<p id="count"></p>

Preview

Todo App — step by step

1 / 4
1

State as the Source of Truth

Store your todo items in a JavaScript array (the state). The UI is a function of that array — never mutate the DOM directly. When state changes, re-render the list from scratch or use diffing to update only changed nodes.

1 of 4
🔍Immutable State Updates Prevent Bugs
Instead of pushing to or splicing the existing array, create a new array each time: spread to add, filter to remove, map to update. Immutable updates make it trivial to implement undo/redo and to compare previous and next state for optimisations.

🎯 Quick Check

What is the recommended way to remove a todo item from a state array?

Next project

Modal Component