DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Todo List App with JavaScript

A todo list app is the classic beginner project for a reason: it teaches you the four operations every app needs — create, read, update, and delete — in a single page you can build in an evening. Add a task, tick it off, delete it, and watch the list stay saved even after you reload the page.

How does it work? The app keeps tasks in an array, renders them as list items, and saves the array to localStorage on every change. When the page loads, it reads the saved tasks back, so nothing is lost.

<!-- index.html -->
<input type="text" id="task-input" placeholder="What needs doing today?" />
<button id="add">Add Task</button>
<ul id="tasks"></ul>
<p id="count"></p>
<button id="clear-done">Clear Completed</button>
Enter fullscreen mode Exit fullscreen mode
// script.js — tasks in an array, saved to localStorage
const input = document.getElementById('task-input');
const list = document.getElementById('tasks');
const countEl = document.getElementById('count');

let tasks = JSON.parse(localStorage.getItem('tasks') || '[]');

function save() {
  localStorage.setItem('tasks', JSON.stringify(tasks));
}

function render() {
  list.innerHTML = '';
  tasks.forEach((task, index) => {
    const li = document.createElement('li');

    const checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    checkbox.checked = task.done;
    checkbox.addEventListener('change', () => {
      tasks[index].done = checkbox.checked;
      save();
      render();
    });

    const span = document.createElement('span');
    span.textContent = task.text;
    if (task.done) span.style.textDecoration = 'line-through';

    const del = document.createElement('button');
    del.textContent = '✕';
    del.addEventListener('click', () => {
      tasks.splice(index, 1);
      save();
      render();
    });

    li.append(checkbox, span, del);
    list.appendChild(li);
  });

  const remaining = tasks.filter((t) => !t.done).length;
  countEl.textContent = `${remaining} of ${tasks.length} tasks left`;
}

function addTask() {
  const text = input.value.trim();
  if (!text) return;
  tasks.push({ text, done: false });
  input.value = '';
  save();
  render();
}

document.getElementById('add').addEventListener('click', addTask);
input.addEventListener('keydown', (e) => {
  if (e.key === 'Enter') addTask();
});
document.getElementById('clear-done').addEventListener('click', () => {
  tasks = tasks.filter((t) => !t.done);
  save();
  render();
});

render();
Enter fullscreen mode Exit fullscreen mode

A todo list is only half the job — the other half is the habit of using it every day. If you want to go deeper into managing your time well, this guide to the best time management books for beginners explains the ideas behind the lists, not just the lists themselves.

This project teaches you the pattern behind almost every web app: keep state in memory, render it to the page, and save it somewhere so it survives a reload. Learn this once, and you can build notes apps, habit trackers, and shopping lists with the same code.

The limits are worth knowing. localStorage only stores about 5 MB and only in one browser, so your tasks will not follow you to your phone. For a shared or synced list, you need a backend — which is a good next project once this one works.

Start today: build the app above, add your three tasks for tomorrow, and reload the page. Seeing them still there is the moment you understand why every app needs storage.

Top comments (0)