Lesson 0002 · ~12 minutes
One skill: use the
Storage method map correctly (strings + JSON,
feature detection, deletes) and predict when a
storage event fires for
sessionStorage vs localStorage.
You can sketch get/set/remove with JSON, name
QuotaExceededError, and correctly say “this will
not notify the other tab” for sessionStorage — without
re-litigating the spectrum.
window.sessionStorage and
window.localStorage each return a
Storage
object. Same methods; different lifetime (lesson 1). Spec surface
(WHATWG HTML):
| Call | Behavior |
|---|---|
getItem(key) |
String, or null if the key is absent |
setItem(key, value) |
Writes a string; may throw
QuotaExceededError
|
removeItem(key) |
Deletes one key (no-op if missing) |
clear() |
Wipes this Storage area |
key(i) / length |
Enumerate; key order is implementation-defined — don’t rely on sort |
sessionStorage.setItem("wizardStep", "2");
const step = sessionStorage.getItem("wizardStep"); // "2" or null
sessionStorage.removeItem("wizardStep");
// sessionStorage.clear(); // nuclear option for this tab's session map
Always use the method API
(setItem/getItem/removeItem),
not sessionStorage.foo = … or bracket assignment. MDN
warns that property access collides with built-ins and invites
prototype pitfalls
(Using the Web Storage API).
Keys and values are always strings. Pass an object without
JSON.stringify and you get the useless
"[object Object]"
(MDN):
const draft = { step: 2, email: "a@b.co" };
// WRONG
sessionStorage.setItem("draft", draft);
sessionStorage.getItem("draft"); // "[object Object]"
// RIGHT
sessionStorage.setItem("draft", JSON.stringify(draft));
const restored = JSON.parse(sessionStorage.getItem("draft"));
// restored is a deep copy — mutating it does not update storage
Guard missing keys: getItem returns null.
Prefer explicit checks over assuming a shape. Catch
setItem failures — full quota or disabled storage both
throw.
The property can exist while writes still fail (policy, private mode quirks). MDN’s pattern actually tries a write (Using guide):
function storageAvailable(type) {
let storage;
try {
storage = window[type];
const x = "__storage_test__";
storage.setItem(x, x);
storage.removeItem(x);
return true;
} catch (e) {
return (
e instanceof DOMException &&
e.name === "QuotaExceededError" &&
storage &&
storage.length !== 0
);
}
}
if (storageAvailable("sessionStorage")) {
sessionStorage.setItem("wizardStep", "1");
}
Call with "sessionStorage" or
"localStorage". Design reviews: never assume Web Storage is
always writable.
storage events — who hears what
A
StorageEvent
is dispatched to other windows that share the same storage
area — not to the document that performed the write
(MDN,
HTML broadcast rules).
Area shared by all same-origin tabs. Other tabs get
storage when one tab writes.
Area shared only within the tab (e.g. same-origin iframes). Other tabs do not get the event — they never shared the map.
window.addEventListener("storage", (e) => {
// e.key, e.oldValue, e.newValue, e.url, e.storageArea
if (e.storageArea === localStorage) {
// react to another tab's localStorage change
}
});
“We’ll sync tabs with sessionStorage + storage events” does not work.
Use localStorage (or BroadcastChannel /
server) for multi-tab messaging. sessionStorage’s job is
isolation, not broadcast.
Related: opening via
window.open
copies sessionStorage once, then the maps diverge — not
a live sync channel
(MDN sessionStorage).
Equal-length options. Pick the best answer; feedback is immediate.
You need to save a small object
{ step: 2, email } in sessionStorage for reload in this
tab.
After getItem("wizardStep"), what should you treat as
“this key was never stored”?
Tab A calls sessionStorage.setItem("x","1"). Tab B is
same origin, no iframes. Who gets a storage event?
Tab A calls localStorage.setItem("theme","dark"). Same
origin Tab B is open. Best expectation?
Before relying on sessionStorage in production, what is the sound availability check?
getItem / setItem /
removeItem / clear / key /
length.
JSON.stringify /
parse; never trust raw object coercion.
QuotaExceededError on write.
storage events: other documents only;
sessionStorage does not cross tabs.
MDN — Using the Web Storage API. Feature detection, set/get/remove, JSON, and StorageEvent with the session-vs-local distinction. Keep the reference sheet open while you code.