A cookie is a small piece of data that a website stores in the user’s browser. The browser automatically sends cookies back to the server with every request to the same domain.
When a user logs in, the server may send:
Set-Cookie: sessionId=abc123; HttpOnly; Secure;
The browser saves this cookie and sends it with future requests:
Cookie: sessionId=abc123
A session is a way for the server to remember a user across multiple requests.
Because HTTP is stateless, the server would otherwise forget everything about a user after each request.
Local storage is a browser feature that lets websites store data persistently on the user’s device. It is part of the Web Storage API.
| Feature | Local Storage |
|---|---|
| Where stored | Browser |
| Lifetime | Until manually cleared |
| Size limit | ~5–10 MB |
| Sent to server | ❌ No |
| Accessible by JS | ✅ Yes |
| HttpOnly | ❌ No |
localStorage.setItem("theme", "dark");const theme = localStorage.getItem("theme");