Skip to content
BENIBIX Learn

Cookies and Local Storage

Level 5 · JavaScript in the page

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 contentsLesson 14 of 14

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

12 mincovers 3 glossary entries

Worth reading first: Fetch API and JSON

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 cookie goes out with every request you send. Local storage stays in the browser.

Worked 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.

The words for this lesson3

Check yourself

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

Finished this lesson?

Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.