# Cookies and Local Storage

> Two ways a site keeps small facts in your browser. Why a cookie goes to the server on every request, and local storage stays put.

**Course:** [HTML, CSS and JavaScript](https://learn.benibix.com/learn/development-web)  
**Time:** 12 min

**After this lesson you can:**

- Say where a cookie goes and where local storage stays
- Pick one of the two for a job
- Name what must never be kept in local storage

## Where a cookie goes, and where local storage stays

A cookie is a small piece of text a site stores in your browser. The browser sends it back to that site with every request. Local storage is a store for a site's data that stays in the browser and is never sent. A cookie holds about 4 kilobytes. Local storage holds about 5 megabytes per site.

A cookie starts on the server. The reply carries a header, a label with a name and a value, called Set-Cookie. From then on, every request to that site carries a Cookie header with the value inside. That is how a site still knows you are logged in when you open the next page.

Cookies carry settings that guard them. HttpOnly hides the cookie from JavaScript, so a harmful script on the page cannot read it. Secure sends it only over an encrypted line. Another setting gives it an end date. A cookie with no end date is deleted when the browser closes.

Local storage is set and read only by JavaScript. Calling setItem("theme", "dark") saves a value, and getItem reads it back next week. It holds only text, so a list is turned into JSON first. Its twin, session storage, is cleared when the tab closes.

The choice follows from where the data needs to be. If the server needs it on every request, such as who is logged in, use a cookie. If only the page needs it, such as a dark theme or a half-written draft, use local storage, and keep the requests small.

Each cookie rides along on every request to its site, even for each picture. A few kilobytes of cookies on a page with 80 files adds up to a lot of extra data. That is one more reason to keep cookies short and few.

People often keep login details in local storage because it is easy. But any script on the page can read it, even one slipped in by an attacker. A login belongs in an HttpOnly cookie, where no script can reach it.

```javascript
// Local storage: stays in this browser, never sent
localStorage.setItem("theme", "dark");
const theme = localStorage.getItem("theme");   // "dark"

// A cookie set by the server arrives in a reply header:
// Set-Cookie: sid=8f3a; HttpOnly; Secure; Max-Age=86400
```
The page sets local storage itself. A login cookie comes from the server and scripts cannot read it.

![A browser on the left and a server on the right. A cookie jar inside the browser sends its cookie along every request arrow to the server. A local storage box inside the browser has no arrow at all and is read only by the page's own script.](https://learn.benibix.com/diagrams/dev-cookie-storage.svg)

### Example: Emma's dark mode slows every page

Emma stored each user's theme and 20 other settings in a cookie of 3 kilobytes. Her pages load 60 files, so each visit sent 180 kilobytes of settings.

She moves the settings into local storage and keeps one short login cookie. The server never needed the theme, and each request is now much smaller.

**Words in this lesson:** [HTTP Cookie](https://learn.benibix.com/term/development/cookie), [Local Storage](https://learn.benibix.com/term/development/local-storage), [HTTP Header](https://learn.benibix.com/term/infrastructure/http-header)

**Question:** Which one does the browser send to the server with every request?

**Answer:** Cookies. A cookie rides along in a header on every request to its site. The other two stay in the browser, and only scripts on the page read them.

---

Source: https://learn.benibix.com/learn/development-web/cookies-and-local-storage
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
