Full answer
All three store data in the browser for one origin (scheme, host, and port), but they differ in lifetime, size, and who can see the data.
localStorage persists after the tab and the browser close. It is shared by every tab of the same origin. The space is typically about 5 MB. The API is setItem, getItem, removeItem, clear, and key. Data never leaves the browser unless the code sends it.
sessionStorage has the same API, but each tab has its own copy, and it is cleared when the tab closes. It suits state for one workflow, such as an unfinished form step.
Cookies are tiny (about 4 KB each) and are added automatically to every matching request to the server. They have an expiry, a path, and flags: HttpOnly hides the cookie from JavaScript, Secure limits it to HTTPS, and SameSite limits cross-site sending. Because of this, cookies are the usual home for session identifiers, and HttpOnly makes them harder to steal.
Web storage stores only strings. Numbers and objects must be converted, with JSON.stringify and JSON.parse. Calls are synchronous, so large reads can block the page. Any script on the page, including a compromised third-party script, can read it, so tokens and personal data do not belong there. Writing can also throw when storage is full or disabled, such as QuotaExceededError in some private modes.
For large or structured client data, IndexedDB is the better tool.
localStorage.clear();
sessionStorage.clear();
localStorage.setItem('theme', 'dark');
localStorage.setItem('visits', 3);
console.log(localStorage.getItem('theme'), localStorage.getItem('visits'), typeof localStorage.getItem('visits'));
console.log(localStorage.getItem('missing'), localStorage.length);
const profile = { name: 'Asha', courses: ['JS', 'React'] };
localStorage.setItem('profile', profile); // stores "[object Object]"
console.log(localStorage.getItem('profile'));
localStorage.setItem('profile', JSON.stringify(profile));
console.log(JSON.parse(localStorage.getItem('profile')).courses);
sessionStorage.setItem('step', '2');
console.log(sessionStorage.getItem('step'), localStorage.getItem('step'));
function readJson(key, fallback) {
try {
return JSON.parse(localStorage.getItem(key)) ?? fallback;
} catch {
return fallback;
}
}
localStorage.setItem('broken', '{not json');
console.log(readJson('broken', 'default'), readJson('nothing', 'default'));
document.cookie = 'consent=yes; path=/';
document.cookie = 'lang=en; path=/';
console.log(document.cookie);
localStorage.removeItem('theme');
console.log(Object.keys(localStorage).sort());Written for a browser console or script tag; checked here with Node.js v24.22.1 and jsdom, two runs with identical output. Output: dark 3 string null 2 [object Object] [ 'JS', 'React' ] 2 null default default consent=yes; lang=en [ 'broken', 'profile', 'visits' ]
Key points
- localStorage persists and is shared across tabs; sessionStorage lasts for one tab.
- Cookies are sent to the server automatically; web storage is not.
- Web storage holds only strings; use JSON for objects.
- HttpOnly, Secure, and SameSite are cookie flags that improve safety.
- Do not store secrets in web storage; use IndexedDB for large data.
Common mistakes
- Storing objects without JSON.stringify and getting "[object Object]".
- Storing authentication tokens or personal data in localStorage.
- Calling JSON.parse on a stored value without a try/catch and a fallback.
- Assuming sessionStorage is shared across tabs of the same site.
Follow-ups
- What does the storage event do, and which tabs receive it?
- How do SameSite=Lax and SameSite=Strict differ?
- When would you pick IndexedDB over localStorage?
Tips
- Compare them on lifetime, size, and "is it sent to the server" in that order.
- Mention that any script on the page can read web storage; it is the key security point.
- Offer HttpOnly cookies for session identifiers.