Skip to content
Velloreth
Web Development 4 min read

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

The comparison

localStoragesessionStorageCookie
Lifetimeuntil clearedthe taban expiry
Scopethe originthe tabthe domain and path
Size~5 MB~5 MB~4 KB
Sent to the servernonoyes
Syncyesyesyes
Readable by JSyesyesunless 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.

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:

AttributeMeaning
HttpOnlynot readable by JavaScript
SecureHTTPS only
SameSitecross-site behaviour
Max-Agethe lifetime
Paththe scope
Domainthe 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

ValueStore
A theme preferencelocalStorage
A form draftsessionStorage
A session tokenan httpOnly cookie
A large datasetIndexedDB
A cache of an APIthe Cache API
A tracking ida 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.

For the related topics, see indexeddb-basics and session-vs-token-authentication. For the broader context, csrf-explained and caching-strategies-guide.

Frequently asked questions

What is the difference between localStorage and sessionStorage?
localStorage persists until the user or the code clears it, and it is shared across every tab of the origin. sessionStorage lasts for the lifetime of the tab, and it is separate per tab. Both store a string and both are synchronous.
Should I store a JWT in localStorage?
It is a common practice and it is risky, because any script on the origin can read it, so a single cross-site scripting flaw exposes the token. An httpOnly cookie is not readable by JavaScript, which removes that path. The trade-off is that a cookie needs CSRF protection.
Why is localStorage synchronous a problem?
Because a read or a write blocks the main thread, so a large value or a frequent write causes jank. IndexedDB is asynchronous and is the right store for a large or frequently written dataset.
What is the size limit?
About 5 MB per origin for localStorage and sessionStorage, and about 4 KB per cookie. The exact limit varies by browser. A larger dataset belongs in IndexedDB.
Are cookies sent with every request?
Yes, for the matching domain and path, which is why a cookie adds to every request and why a large cookie is a performance problem. A cookie is also sent on a cross-site request unless SameSite restricts it.
What is the storage event?
An event fired on the other tabs of the same origin when localStorage changes, which is how a tab can react to a change in another tab. sessionStorage does not fire it, because it is per tab.