Learn how to use the JavaScript localStorage API to persist data in the browser between page reloads and visits.
Day 25 of 30 | Stage: Async, OOP, and the Real World | Estimated time: 40-50 minutes
- Store and retrieve data with localStorage
- Persist objects and arrays using JSON.stringify and JSON.parse
- Understand the limitations of localStorage
- Sync application state with the browser's storage
localStorage lets you save key-value data in the browser that persists even after the page is closed or reloaded, unlike regular JavaScript variables, which reset every time.
localStorage.setItem("username", "Theo");
console.log(localStorage.getItem("username")); // "Theo"
localStorage.removeItem("username");
console.log(localStorage.getItem("username")); // null
localStorage.setItem("a", "1");
localStorage.setItem("b", "2");
localStorage.clear(); // removes everythinglocalStorage can only store strings. To store objects or arrays, convert them with JSON.stringify(), and convert them back with JSON.parse().
const user = { name: "Sora", age: 27 };
localStorage.setItem("user", JSON.stringify(user));
const storedUser = JSON.parse(localStorage.getItem("user"));
console.log(storedUser.name); // "Sora"function getStoredTodos() {
const data = localStorage.getItem("todos");
return data ? JSON.parse(data) : [];
}
function saveTodos(todos) {
localStorage.setItem("todos", JSON.stringify(todos));
}
let todos = getStoredTodos();
todos.push({ text: "Learn localStorage", done: false });
saveTodos(todos);function render() {
saveTodos(todos); // persist on every change
// ... re-render the DOM from `todos` as usual
}By saving inside the same function that re-renders the UI, your data and your screen stay in sync, and now also survive a page reload.
- Storage is limited to roughly 5 to 10 MB per site, varying by browser.
- Data is stored as plain text, so never store sensitive information like passwords.
- Storage is specific to a browser and device, it does not sync across devices automatically.
sessionStorageis nearly identical, but clears when the browser tab closes, useful for temporary, per session data.
Always wrap stored JSON in a fallback when reading it back, since
localStorage.getItem()returnsnullfor missing keys, and passingnulltoJSON.parse()throws an error. Never store sensitive data such as passwords or tokens that should remain secret inlocalStorage, since it is plain text and accessible to any script running on the page.
localStoragepersists string data in the browser across reloads and visitsJSON.stringify()/JSON.parse()allow storing and retrieving objects and arrays- Always provide a fallback when reading potentially missing keys
localStoragehas size limits and should never hold sensitive data
Save your name to localStorage under the key "visitorName", reload the page mentally (or actually), and write code that greets the visitor by name if it exists, or asks for it if not.
Upgrade the Day 15 to-do list so tasks are saved to localStorage and survive page reloads.
Open the Day 25 project brief →
← Day 24: Fetch API and JSON · Course Home · Day 26: Classes and OOP →