localStorage: key-value data kept in the browser until it is cleared. About 5 MB, same origin only, never sent to the server automatically.
sessionStorage: same API, but cleared when the tab is closed.
Cookies: small (about 4 KB), sent to the server with every matching request. They can have expiry, Secure, SameSite and HttpOnly flags.
For a theme preference, localStorage is perfect.
For a login token, prefer an HttpOnly, Secure, SameSite cookie. JavaScript cannot read an HttpOnly cookie, so an XSS bug cannot steal it. Anything in localStorage can be read by any script running on your page. If you do use cookies, protect against CSRF with SameSite and anti-forgery tokens.
Never store passwords or sensitive personal data in any of them.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
For a theme preference, localStorage is perfect.
For a login token, prefer an HttpOnly, Secure, SameSite cookie. JavaScript cannot read an HttpOnly cookie, so an XSS bug cannot steal it. Anything in localStorage can be read by any script running on your page. If you do use cookies, protect against CSRF with SameSite and anti-forgery tokens.
Never store passwords or sensitive personal data in any of them.