Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 36 ยท Application and Storage

Cookies, Local Storage and Session Storage

Read the cookie table in DevTools, understand HttpOnly, Secure and SameSite, see why cookies are sent to servers, and compare Local storage with Session storage.

Cookies and the Web Storage API (Local storage and Session storage) are the simplest ways for a site to remember things. They also cause some of the most confusing bugs: a login that "randomly" stops working, a cookie that is set but never sent, a setting that survives when it should not. DevTools lets you see exactly what is stored and, for cookies, which attributes control their behavior.

This lesson teaches you to read the Cookies table, explains each attribute, and compares the three storage types.

The Cookies table

In Application > Cookies, choose the site. The table shows one row per cookie with these columns:

Column Meaning
Name / Value The data. Values are often encoded or signed, so they look random.
Domain Which hosts receive the cookie. A leading dot means subdomains too.
Path Only URLs under this path receive it.
Expires / Max-Age When it is deleted. "Session" means when the browser closes.
Size Name plus value in bytes. One cookie is limited to about 4 KB.
HttpOnly Check mark: JavaScript cannot read it via document.cookie.
Secure Check mark: sent only over HTTPS.
SameSite Strict, Lax or None: controls whether it is sent on cross-site requests.
Priority, Partition Key Eviction priority and the top-level site for partitioned (CHIPS) cookies.

Click a row to see the decoded value at the bottom. If a cookie is dimmed or has a yellow warning icon, hover for the reason: for example, a cookie marked SameSite=None without Secure is rejected by modern browsers.

The Network panel helps with the other half: select a request, open the Cookies tab, and you see which cookies were sent and which were set by the response, including ones that were blocked with a reason such as "SameSite=Lax" or "Secure attribute". This is the first place to look when a cookie seems to disappear.

Why attributes matter

  • HttpOnly protects session cookies from theft by injected scripts (XSS). Login cookies should have it.
  • Secure stops a cookie from leaking over plain HTTP.
  • SameSite=Lax (the default in modern browsers) sends the cookie on top-level navigations such as clicking a link, but not on cross-site subrequests like images or fetch calls. Strict never sends it cross-site. None sends it everywhere and requires Secure; use it only for cookies that really need to work in third-party contexts.
  • Domain and Path decide who receives it. Setting Domain=example.com shares the cookie with every subdomain, which may be more than you want.

A cookie is automatically attached to every matching request, including images, which is why large cookies slow pages and why session cookies are not placed on static-asset domains.

The stand-in below parses a Set-Cookie response header into the same columns you see in DevTools and warns about common mistakes.

JS Stand-in: parse and lint a Set-Cookie header

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Local storage and Session storage

These two share the same API (getItem, setItem, removeItem, clear, key, length) and the same limits (about 5 MB, strings only). The differences:

Local storage Session storage
Lifetime Until cleared Until the tab is closed
Shared between tabs Yes, same origin No, each tab has its own copy
Sent to the server Never Never
Good for Theme, preferences, drafts Wizard state, one-tab flows

Neither is a safe place for secrets: any script on the page, including injected or third-party scripts, can read them. Use HttpOnly cookies for session identifiers.

In the panel you see a two-column table. You can double-click to edit, add rows, and use the filter. Local storage changes in one tab trigger a storage event in the other tabs, which is a simple way to sync state between tabs.

The sandboxed previews on this site cannot use the real storage APIs, so the stand-in below implements the same interface in memory to show typical operations, including why values must be serialized.

JS Stand-in: a Storage-like object with JSON

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first prefs value is a classic bug you will recognize in the panel: a row whose value is [object Object].

Debugging recipes

  • Login lost after redirect: look at the response's Set-Cookie in the Network panel > Cookies tab for a blocked reason; fix SameSite or Secure.
  • Cookie not sent to API on another domain: the request needs credentials: "include" and the cookie needs SameSite=None; Secure, and the server must reply with specific CORS headers.
  • Setting does not persist: check that code writes to Local storage (not Session) and that the value is a string.
  • Stale data after deploy: version your keys (prefs_v2) or clear the key on startup.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which cookie attribute stops JavaScript from reading the cookie?

    HttpOnly hides the cookie from document.cookie, which protects session cookies from XSS theft.
  2. 2. A cookie has SameSite=None but no Secure attribute. What happens in modern Chrome?

    SameSite=None requires Secure; otherwise the cookie is rejected.
  3. 3. How is Session storage different from Local storage?

    Session storage is per tab and per session. Neither storage type is sent to the server.
  4. 4. Where can you see why a cookie was blocked on a specific request?

    The request's Cookies tab shows sent and set cookies and any blocked reasons.

Summary

  • The Cookies table exposes Domain, Path, Expires, HttpOnly, Secure and SameSite; the Network panel shows what was sent or blocked.
  • HttpOnly and Secure protect session cookies; SameSite controls cross-site sending.
  • Local storage persists and is shared across tabs; Session storage is per tab; both hold strings only and are readable by any script.
  • Serialize objects with JSON and version your keys.

Next: Service Workers and Cache Storage