localStorage vs sessionStorage vs Cookies: What to Use
Three browser stores, three lifetimes. Learn the scope, the size, the security, and which one fits a given value.
By Velloreth
The comparison
| localStorage | sessionStorage | Cookie | |
|---|---|---|---|
| Lifetime | until cleared | the tab | an expiry |
| Scope | the origin | the tab | the domain and path |
| Size | ~5 MB | ~5 MB | ~4 KB |
| Sent to the server | no | no | yes |
| Sync | yes | yes | yes |
| Readable by JS | yes | yes | unless httpOnly |
localStorage
localStorage.setItem('theme', 'dark');
const theme = localStorage.getItem('theme');
localStorage.removeItem('theme');
localStorage.clear();
It persists until it is cleared.
It is shared across every tab of the origin.
It stores a string, so an object needs a serialisation.
localStorage.setItem('user', JSON.stringify(user));
const user = JSON.parse(localStorage.getItem('user'));
sessionStorage
sessionStorage.setItem('draft', text);
It lasts for the lifetime of the tab.
It is separate per tab, so two tabs have two values.
A reload keeps it, and a new tab does not.
It is the right store for a form draft or a one-time flow.
The cookie
document.cookie = 'theme=dark; path=/; max-age=31536000; SameSite=Lax';
It is sent with every matching request, which is why it is used for a session.
The attributes:
| Attribute | Meaning |
|---|---|
HttpOnly | not readable by JavaScript |
Secure | HTTPS only |
SameSite | cross-site behaviour |
Max-Age | the lifetime |
Path | the scope |
Domain | the scope |
HttpOnly is the key one for a token, because it removes the JavaScript read path.
The security
localStorage and sessionStorage are readable by any script on the origin.
A single XSS flaw exposes every value.
A cookie with HttpOnly is not readable by JavaScript, which is the safer place for a session token.
The trade-off: a cookie is sent automatically, so it needs CSRF protection.
The rule: a session token belongs in an HttpOnly, Secure, SameSite cookie, and a non-sensitive preference belongs in localStorage.
The synchronous cost
A read or a write blocks the main thread.
A large value or a frequent write causes jank.
IndexedDB is asynchronous and is the right store for a large or frequently written dataset.
A write on every keystroke is a common mistake.
The storage event
window.addEventListener('storage', (event) => {
if (event.key === 'theme') applyTheme(event.newValue);
});
It fires on the other tabs of the same origin.
It is how a tab reacts to a change in another tab, such as a logout.
sessionStorage does not fire it, because it is per tab.
The quota
About 5 MB per origin for localStorage and sessionStorage.
About 4 KB per cookie.
A write over the quota throws, so it must be caught.
try {
localStorage.setItem('big', data);
} catch (e) {
// QuotaExceededError
}
The private mode
A private window may block or limit the storage.
A read may throw, so the access must be guarded.
function safeGet(key) {
try { return localStorage.getItem(key); } catch { return null; }
}
The use cases
| Value | Store |
|---|---|
| A theme preference | localStorage |
| A form draft | sessionStorage |
| A session token | an httpOnly cookie |
| A large dataset | IndexedDB |
| A cache of an API | the Cache API |
| A tracking id | a cookie |
Common mistakes
A token in localStorage. An XSS exposes it.
A cookie without HttpOnly. The same risk.
A cookie without SameSite. A CSRF risk.
A cookie without Secure. It is sent over HTTP.
A large value in localStorage. A jank.
A write on every keystroke. A jank.
No quota handling. A thrown error.
No private-mode guard. A thrown error.
An object stored without a serialisation. [object Object].
A secret in a cookie. It is sent to the server.
A quotable summary
localStorage persists until it is cleared and is shared across the tabs of an origin, sessionStorage lasts for the lifetime of a tab, and a cookie is sent with every matching request. Both web storage APIs are synchronous and readable by any script on the origin, so a session token belongs in an HttpOnly, Secure, SameSite cookie, and a non-sensitive preference belongs in localStorage. A large or frequently written dataset belongs in IndexedDB.
Related reading
For the related topics, see indexeddb-basics and session-vs-token-authentication. For the broader context, csrf-explained and caching-strategies-guide.