Your To-Do App Loses Tasks on Refresh? It's Missing Two Lines
Every beginner builds a to-do app. And almost every beginner version has the same bug: you add five tasks, reload the page, and all five are gone. The app looks finished. Add works, tick works, delete works. But nothing
Every beginner builds a to-do app. And almost every beginner version has the same bug: you add five tasks, reload the page, and all five are gone.
The app looks finished. Add works, tick works, delete works. But nothing survives a refresh, because the tasks only live in a JavaScript array — and an array lives only as long as the page does. Reload, and the browser throws it away and starts over with an empty one.
The fix is two lines, and they belong in every to-do app you write from now on.
Save after every change, load before the first render
localStorage is the browser's built-in key-value store. Anything you put there stays on the user's computer until your code removes it. But it only stores strings, so an array has to be converted first:
function save() {
localStorage.setItem('tasks', JSON.stringify(tasks));
}
function load() {
const stored = localStorage.getItem('tasks');
if (stored) {
tasks = JSON.parse(stored);
}
}
load();
render();
JSON.stringify turns the array into a string before saving. JSON.parse turns it back into a real array when loading. Skip that pair and save the array directly, and JavaScript silently converts it to the string "[object Object],[object Object]" — your data is gone, with no error to tell you why.
The order matters too. Call load() before the first render(), so the saved tasks are in the array before the screen is drawn. And call save() after every change — add, toggle, delete — not just when the page unloads. Saving on every change is simpler and it never misses a case.
The delete bug that comes next
Once saving works, the next bug shows up in delete. Most first versions delete by array position:
// Don't do this
tasks.splice(index, 1);
It works once. Then the indexes shift: after you remove task 2, the old task 3 is now at index 2. Delete by index again and you remove the wrong task. The fix is a stable id on each task — Date.now() is enough for this app — and filtering by it:
tasks = tasks.filter((t) => t.id !== id);
One more rule: render from state, always
The pattern that holds all of this together is simple: one array is the truth, one render() function draws the screen from that array, and every action is change the array, save, render. Never edit a list item directly to change a task. The moment the screen and the array disagree, the next render or save overwrites one of them, and the bug looks random.
I wrote the full step-by-step build — HTML skeleton, CSS, state and render, add with Enter key, tick and delete with event delegation, the localStorage save/load, the four beginner mistakes, and the complete app.js in one file — here:
https://rajadevsolutions.online/blog/how-to-build-a-simple-to-do-app-in-javascript
It is plain JavaScript, no framework and no build step. Open index.html in a browser and it works. And once the basic version runs, the same pattern scales: filters, edit-in-place, due dates — all of them are just the same three steps on a slightly bigger array.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.