Browser Storage, Cookies & Sessions
Cookies, localStorage, sessionStorage, and IndexedDB compared, including why storing an auth token safely is harder than it looks.
Browser Storage, Cookies & Sessions
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Cookies are the only storage option automatically sent back to the server on every matching request — localStorage and sessionStorage never leave the browser on their own.
- localStorage persists until something explicitly clears it; sessionStorage is wiped when the tab closes; both are capped around 5-10MB and only hold strings.
- IndexedDB is a real in-browser database — indexed, asynchronous, and able to hold megabytes of structured data or binary blobs, for cases the other three are too small or too simple for.
- Any JavaScript running on your page — including an attacker's, via XSS — can read localStorage and sessionStorage; an httpOnly cookie is invisible to JavaScript entirely, by design.
- This is why a session token is usually safer in an httpOnly cookie than in localStorage: the tradeoff isn't convenience versus inconvenience, it's convenience versus theft if the page is ever compromised by injected script.
The problem: HTTP forgets everything
HTTP is stateless by design — every request is handled as if the server has never seen you before. That’s a fine model for serving static documents, but it breaks down the instant you need a shopping cart to survive a page reload, or a user to stay logged in across requests. The browser needs somewhere to keep data around between page loads, and depending on what that data is and who needs to see it, you reach for a different tool.
Cookies: the one the server sees automatically
A cookie is a small piece of text — typically under 4KB — stored by the browser and associated with a domain. Its defining feature is that the browser attaches it automatically to every HTTP request sent to a matching domain, without any JavaScript involved. That’s exactly what made cookies the original mechanism for login sessions: the server sets a cookie containing a session ID once, and from then on every request arrives with that ID already attached, letting the server recognize you.
Cookies can be scoped with flags that matter a lot in practice: Secure means “only send this over HTTPS,” SameSite restricts whether it’s sent on cross-site requests (a key defense against a class of attacks called CSRF), and httpOnly — covered below — hides the cookie from JavaScript entirely.
localStorage and sessionStorage: client-only, string-only
localStorage and sessionStorage share an API — setItem, getItem, removeItem — and both live entirely in the browser; nothing you put there is ever sent to a server unless your code explicitly includes it in a request. The difference between them is lifetime: localStorage persists indefinitely, surviving tab closes and browser restarts, until something — your code, or the user clearing site data — deletes it. sessionStorage is scoped to one tab and disappears the instant that tab closes.
Both are capped around 5-10MB depending on the browser, and both only store strings — an object has to be JSON.stringify’d going in and JSON.parse’d coming out. That makes them a good fit for small, disposable client-side state: a theme preference, a draft form’s contents, a “don’t show this banner again” flag. They’re a poor fit for anything large, structured, or sensitive.
IndexedDB: an actual database, in the browser
When you need more than a few kilobytes of structured data — offline product catalogs, cached API responses, binary files — localStorage’s synchronous, string-only model falls apart. IndexedDB is the browser’s answer: a real transactional database, built into every modern browser, that stores structured records (not just strings), supports indexes for fast lookups, holds binary data like images and files, and does everything asynchronously so large reads and writes don’t freeze the page. It has a famously clunky raw API, which is why most apps that use it reach for a wrapper library — but conceptually, it’s the tool for “the data is too big or too structured for the other three.”
The question that actually matters: where do you put a session token?
This is where the choice between these options stops being about convenience and starts being about security. Say your app issues a JWT after login, and you need to attach it to every subsequent API request. The obvious move is localStorage.setItem('token', jwt) — it’s simple, and you can read it back from anywhere in your JS. It’s also the riskier choice, for one specific reason: any JavaScript that runs on your page can read localStorage, including JavaScript that shouldn’t be there at all.
If your site has a cross-site scripting (XSS) vulnerability anywhere — a comment field that doesn’t escape input, a dependency with a compromised build — an attacker’s injected script can run localStorage.getItem('token') just as easily as your own code can, and quietly send that value to their own server. From that moment on, they can impersonate the user anywhere that token is accepted, with no further access to the victim’s machine needed.
An httpOnly cookie sidesteps this specific threat by design: it’s a cookie flag that makes the cookie’s value invisible to document.cookie and to every other JavaScript API. The browser still attaches it automatically to matching requests, so the server-side auth flow works identically — but an injected script, no matter how it got there, cannot read the token’s value at all. It isn’t a complete defense against every attack (a cookie without SameSite set correctly is still exposed to CSRF, which is a different problem with a different fix), but against the specific, common threat of “a script got onto my page somehow,” it closes a door that localStorage leaves wide open. That’s the real tradeoff underneath “where do I put the token”: convenience and simplicity on one side, resilience to a single compromised script on the other.
Check yourself
If you store a value in sessionStorage, what happens to it when the user closes the tab?
It's gone — sessionStorage is scoped to a single tab's lifetime and is cleared the moment that tab or window closes, unlike localStorage, which survives.
Why is a cookie different from localStorage in terms of what the server sees?
A cookie marked for a given domain and path is attached automatically to every matching HTTP request the browser makes to that server, with no JavaScript needed — localStorage data never leaves the browser unless your code explicitly sends it.
Why would you reach for IndexedDB instead of localStorage for an offline-capable app caching thousands of records?
localStorage is synchronous, string-only, and capped at a few megabytes, which makes it a poor fit for large or structured data — IndexedDB is asynchronous, supports indexes and binary data, and scales to much larger datasets.
If an attacker manages to inject a script into your page (XSS), what can they do to a JWT stored in localStorage that they can't do to one stored in an httpOnly cookie?
They can read it directly — localStorage.getItem() — and exfiltrate it to their own server, impersonating the user anywhere that token is accepted; an httpOnly cookie is invisible to JavaScript, so the same injected script can't read the token's value at all.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.