Memory after a nap
setItem, getItem, removeItem, strings only, JSON, null for empty keys, and what not to put in the browser.
The browser has one — `localStorage`. It's a "key → string" store that the browser keeps on disk, separately for every site. The data stays until someone deletes it: reloads, closed tabs and even a restarted computer don't scare it.
There are only a few methods:
localStorage.setItem(key, value)— put;localStorage.getItem(key)— get;localStorage.removeItem(key)— delete one key;- localStorage.clear() — delete everything this site stored.
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme"));
console.log(localStorage.getItem("bowl"));
localStorage.removeItem("theme");
console.log(localStorage.getItem("theme"));
document.querySelector("#note").textContent = "Theme saved and removed";getItem returns null, not undefined. In the sandbox, storage is empty on every run.localStorage.setItem("lives", 9);
const lives = localStorage.getItem("lives");
console.log(typeof lives);
console.log(lives + 1);
console.log(Number(lives) + 1);So how do you save an object or an array? Pack it into a string yourself — with JSON, which you already know from the errors chapter. JSON.stringify turns data into text, JSON.parse turns text back into data. Without it, an object becomes a useless "[object Object]".
const cat = { name: "Byte", toys: ["mouse", "yarn"] };
localStorage.setItem("cat", cat);
console.log(localStorage.getItem("cat"));
localStorage.setItem("cat", JSON.stringify(cat));
console.log(localStorage.getItem("cat"));
const saved = JSON.parse(localStorage.getItem("cat"));
console.log(saved.toys[1]);Two nasty cases remain.
- The first visit: the key doesn't exist yet, and getItem returned null. You need a default:
localStorage.getItem("theme") ?? "light". - Corrupted data: someone (an old version of the site, an extension, the user in DevTools) put non-JSON there. Then JSON.parse throws a
SyntaxError— and without try...catch the page crashes on load. Every time.
function load(key, fallback) {
const text = localStorage.getItem(key);
if (text === null) return fallback;
try {
return JSON.parse(text);
} catch {
return fallback;
}
}
localStorage.setItem("cart", "{broken");
console.log(load("cart", []));
console.log(load("favorites", ["fish"]));
localStorage.setItem("cart", "[1, 2]");
console.log(load("cart", []));A few more rules from real projects:
- Namespace your keys:
"tasky:theme", not"theme". Your code, libraries and widgets all live on one site — short keys are easy to overwrite. - Version your data: formats change. Save
{ version: 2, ... }or merge with defaults:{ ...defaults, ...saved }— then new fields get values even when an older version was saved. - The limit is about 5 MB per site. When there's no room, setItem throws a QuotaExceededError.
- Relatives: sessionStorage — the same thing, but it only lives until the tab is closed; cookies — tiny (about 4 KB) and sent to the server with every request; the server — the only place where data is visible from another device.
Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.