---
title: "What is the difference between localStorage, sessionStorage and cookies?"  
description: "What is the difference between localStorage, sessionStorage and cookies?"  
author: "Mikko Lehtonen"  
published: 2026-11-13  
updated: 2026-11-13  
canonical: https://www.mindstick.com/forum/163015/what-is-the-difference-between-localstorage-sessionstorage-and-cookies  
category: "JavaScript and Frontend"  
tags: ["localStorage", "sessionStorage", "cookies", "browser storage", "web security"]  
reading_time: 1 minute  

---

# What is the difference between localStorage, sessionStorage and cookies?

Where should I store the user's theme preference, and where should I store a login token?

## Replies

### Reply by Ivan Nikolaev

- **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.


---

Original Source: https://www.mindstick.com/forum/163015/what-is-the-difference-between-localstorage-sessionstorage-and-cookies

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
