localStorage vs cookies — where to store user data

People usually compare these by capacity: a cookie holds about 4 kilobytes, localStorage about five megabytes. A thousand-fold difference, case closed, take localStorage.
That's not what decides it. This is: cookies travel to the server on every single request, while localStorage never leaves the browser until you send it yourself.
Everything else follows from that one line — including why the login token usually goes in a cookie.
What they actually are
Both are ways to keep something in the user's browser so the next visit doesn't start from scratch.
- A cookie is a small note the browser attaches to every request to your domain. Automatically, without a line of code. The server reads it and knows who showed up.
- localStorage is a key-value store inside the browser. It lives purely on the user's side. The server knows nothing about it until your frontend puts a value into a request.
They're written differently too:
// localStorage — two lines, all in the browser
localStorage.setItem("theme", "dark");
localStorage.getItem("theme"); // "dark"
// cookie — usually set by the server, in a response header
// Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Lax
An honest comparison
| Criterion | Cookie | localStorage |
|---|---|---|
| Sent to the server | yes, automatically on every request | no, only if you send it by hand |
| Who can read it | the server and JS (unless HttpOnly) | only JS in the browser |
| Size | ~4 KB each, dozens per domain | ~5 MB per site |
| Lifetime | you set it: an hour, a month, until the browser closes | forever, until manually cleared |
| Can be hidden from JS | yes — the HttpOnly flag | no, any script on the page can read it |
| Available during server rendering | yes | no: it doesn't exist on the server |
| Convenience | more ceremony | two lines of code |
Two rows in that table outweigh the rest.
HttpOnly. JavaScript cannot read a cookie with that flag — at all. If a foreign script somehow lands on your page (say, through a vulnerable dependency), it can scrape everything out of localStorage, session token included, and ship it anywhere. It can't touch an HttpOnly cookie.
Available during server rendering. This is the daily pain on Next.js and friends: the server renders the page, and localStorage doesn't exist there. That's why a dark theme stored in localStorage flashes white on load, while a theme stored in a cookie doesn't — the server knows it before painting.
Who should use what
No fence-sitting:
- Login and sessions — cookies. With
HttpOnly,SecureandSameSite=Lax. Every grown-up auth framework does it this way, and it's not fashion, it's protection against a script stealing the token. More in the guide on adding login. - Theme, language, "has seen onboarding," form drafts — localStorage. The server doesn't need to know, size is irrelevant, it's two lines of code.
- Theme, if you render on the server — cookie. Precisely to kill that white flash.
- A pre-login cart — localStorage. But if the server calculates prices and discounts, store product ids only, not prices.
- Analytics and UTM tags — cookies. The server has to see them anyway.
And one rule on top of all of it: nothing secret in either. A user opens both in DevTools in two seconds and edits them by hand. isAdmin: true in localStorage isn't a permission check, it's an invitation. Only the server checks permissions.
A short note on size
The 4-kilobyte figure matters less as a limit and more as bandwidth: cookies ride along with every request, including images and stylesheets. Stuff half a kilobyte of settings in there and every request on your site gets half a kilobyte heavier. On mobile that's noticeable.
So cookies get short things: a session id, a language tag, a theme flag. Anything bulky belongs in localStorage or on the server.
What about sessionStorage?
Same as localStorage, but it lives until the tab closes and each tab has its own. Handy for things that shouldn't survive a close: a wizard step, filter state, a one-off form draft.
Do I need consent for cookies?
Depends what they're for. Strictly necessary ones (session, security, language choice) usually don't need separate consent. Analytics and advertising ones do, and the rules differ by country. Key nuance: the law talks about purpose, not technology — hiding tracking in localStorage instead of a cookie doesn't sidestep the requirements.
What happens if the user clears their browser?
Both vanish: login drops, theme resets, cart empties. Anything that must survive a wipe or move to another device belongs on the server in a database, with the browser holding only a copy for speed.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





