Cookies vs localStorage vs sessionStorage in JavaScript

Compare cookies, localStorage, and sessionStorage side by side. Learn which storage mechanism to use for authentication, preferences, caching, and temporary data.

6 min read

The browser gives you three ways to store data on the client: cookies, localStorage, and sessionStorage. They look similar from a distance, but each one serves a fundamentally different purpose. Choosing the wrong one leads to security problems, performance issues, or data that disappears when you expect it to persist.

Here is the decision in one sentence: use cookies when the server needs the data, localStorage when data should survive forever, and sessionStorage when data should die with the tab.

Side-by-side comparison

The differences become clear when you put the three mechanisms in a single table. Pay attention to lifetime and server access, because those two columns drive most real-world decisions.

CookieslocalStoragesessionStorage
CapacityAbout 4 KB per cookieAbout 5 MB per originAbout 5 MB per origin
LifetimeConfigurable (session or persistent)Forever, until manually deletedUntil tab closes
Sent to serverYes, with every HTTP requestNoNo
Shared across tabsYesYesNo (each tab isolated)
Accessible via JSYes (unless HttpOnly)YesYes
API styleString parsing on document.cookieSimple key-value methodsSame as localStorage
Secure context requiredNo (Secure flag optional)No (but recommended)No (but recommended)
HttpOnly flagYes (set by server)Not applicableNot applicable

Each difference matters for a specific use case. Capacity matters when storing large JSON. Server access matters for authentication. Tab isolation matters for multi-tab workflows.

When to use cookies

Cookies are the only storage mechanism that the browser automatically attaches to HTTP requests. Every time the browser requests a page, image, or API endpoint from your server, it includes all cookies for that domain in the request headers.

This automatic server access makes cookies the right choice for authentication tokens. The server receives the session cookie with every request and knows who the user is without any client-side code. Combined with the HttpOnly flag set by the server, cookies protect tokens from cross-site scripting attacks because JavaScript cannot read HttpOnly cookies at all.

Cookies also support server-controlled expiration dates, domain scoping to include or exclude subdomains, and the SameSite attribute to prevent cross-site request forgery. Use cookies when the server is the primary consumer of the data.

When to use localStorage

localStorage is the simplest storage API and the best choice for data that only matters on the client side. User interface preferences like theme, font size, and language belong in localStorage. Cached API responses that prevent redundant network requests also fit here.

The 5 MB capacity is generous enough for most client data. Since localStorage is never sent to the server, it does not add overhead to every HTTP request like cookies do. Data persists until your code explicitly removes it or the user clears their browser data.

Use localStorage for information that should survive across sessions: a shopping cart for an unauthenticated user, onboarding flags that track whether a tutorial has been seen, or a dashboard layout that the user customized.

When to use sessionStorage

sessionStorage is localStorage with one critical difference: the data is scoped to a single tab and disappears when that tab closes. Each tab gets its own isolated sessionStorage, so opening the same site in two tabs gives each tab an independent storage space.

This isolation makes sessionStorage ideal for temporary state. Multi-step form wizards store partial data without worrying about cross-tab interference. One-time security tokens can live in sessionStorage where closing the tab guarantees they are gone. Page state like scroll position or expanded accordion sections persists across refreshes but resets on the next visit.

Use sessionStorage when you want localStorage's simple API but need the data to clean itself up automatically.

Security considerations

Storage security is about access control. Any JavaScript running on your page can read localStorage and sessionStorage. This means a cross-site scripting vulnerability exposes everything in both stores to an attacker.

Cookies with the HttpOnly flag are invisible to JavaScript. An XSS attack cannot steal them because document.cookie does not include HttpOnly cookies. For authentication tokens, this is the critical difference. Always set HttpOnly, Secure, and SameSite=Lax on session cookies from the server.

For sensitive client data in localStorage, consider encrypting values before storage. This does not prevent XSS from reading the encrypted data, but it protects the plaintext if the user's device is compromised outside the browser.

Practical decision flowchart

Here is a simple way to decide which storage mechanism fits your use case. Start with the top question and follow the answer.

Choosing browser storage: cookies, localStorage, or sessionStorage

If the server reads the data, the answer is always cookies. If the data is client-only and should persist, use localStorage. If the data is client-only and temporary, sessionStorage wins. The flowchart covers nearly every real-world decision.

Common mistakes

  • Storing authentication tokens in localStorage. They are accessible to any JavaScript on the page, making XSS attacks far more dangerous. Use HttpOnly cookies.
  • Using cookies for large data. The 4 KB limit per cookie and the overhead of sending cookies with every request make them a poor choice for storing JSON blobs.
  • Expecting sessionStorage to be shared across tabs. Each tab is isolated. Use localStorage with manual cleanup if you need cross-tab shared temporary data.
  • Storing sensitive unencrypted data in localStorage. It is readable by any script on the page and persists on disk indefinitely.
  • Creating too many cookies. Browsers limit the total number of cookies per domain. Consolidate related data into fewer cookies when possible.

Quick reference

DecisionUse
Server needs the dataCookies (with HttpOnly from server)
Data survives forever, client onlylocalStorage
Data dies with the tab, client onlysessionStorage
Security-sensitive tokenCookies with HttpOnly + Secure + SameSite
Large structured data (> 5 MB)IndexedDB, not any of these three
Rune AI

Rune AI

Key Insights

  • Cookies are sent to the server automatically. Use them for authentication and server-read data.
  • localStorage persists until explicitly deleted. Use it for user preferences and cached data.
  • sessionStorage clears when the tab closes. Use it for form drafts and temporary state.
  • Cookies are limited to about 4 KB. localStorage and sessionStorage allow about 5 MB.
  • HttpOnly cookies are invisible to JavaScript, making them the safest choice for session tokens.
RunePowered by Rune AI

Frequently Asked Questions

Which storage mechanism should I use for authentication tokens?

Use cookies with the HttpOnly, Secure, and SameSite flags set by the server. HttpOnly prevents JavaScript from reading the token, protecting it from XSS attacks. localStorage is accessible to any script on the page, making it less secure for sensitive tokens.

Can I use all three storage mechanisms at the same time?

Yes. Many apps use cookies for authentication, localStorage for user preferences like theme and language, and sessionStorage for temporary form state. They serve different purposes and do not conflict.

How do I clear all three storage types on logout?

Clear localStorage with localStorage.clear(), sessionStorage with sessionStorage.clear(), and cookies by setting each one's expiration to the past with the correct path. Server-side session invalidation is separate from client-side storage clearing.

Is IndexedDB better than these three options?

IndexedDB is a full transactional database in the browser with much larger storage limits (often hundreds of MB). Use it for structured data, offline apps, and large datasets. localStorage and cookies are simpler and better for small key-value data.

Conclusion

Cookies, localStorage, and sessionStorage each solve a different problem. Cookies are for server communication and secure tokens. localStorage is for persistent client-side data that survives tab close. sessionStorage is for temporary data scoped to a single tab. Choose based on lifetime, server access needs, and capacity requirements, not on API preference.