Cookies vs localStorage
Both save values in the browser. Cookies travel to the server with every request and are used for login, while localStorage stays only in the browser and is used to remember screen settings.
Cookies & Sessions
A small note passed back and forth with the server. Used to keep you logged in.
localStorage
A value kept in the browser's drawer. It never goes to the server.
At a glance
| Cookies & Sessions | localStorage | |
|---|---|---|
| Goes to the server? | Yes, with every request | No |
| Size | Very small (about 4KB) | Fairly big (about 5MB) |
| Security | Can be hidden with httpOnly | Any JavaScript can read it |
| Good for | Login | Screen settings, temporary data |
Use “Cookies & Sessions” when…
- When you need to keep someone logged in
- When the server needs to know who this person is
- When you handle values where security matters (httpOnly)
🤖 Say it to your AI like this
Store the login session in an httpOnly, secure cookie. Make sure JavaScript can't read it, and delete it when the user logs out.
Use “localStorage” when…
- When you want to remember screen settings like dark mode or language
- When you want to keep recently viewed items or a cart without logging in
- When the value doesn't need to be sent to the server
🤖 Say it to your AI like this
Save the dark mode setting in localStorage so it stays the next time someone visits. Make sure it doesn't throw errors in browsers that block storage.
💡 How not to mix them up
Don't keep sensitive values like passwords or tokens in localStorage. For login, an httpOnly cookie is safer.