Skip to article content
FrontendXLesson 11 / 20

Browser Storage APIs

Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.

intermediate · about 40 minutes · 2 exercises

  • Predict
  • Explain

What you will be able to do

#
  • Compare cookies, Web Storage, IndexedDB, and Cache Storage.
  • Choose the right storage for a task.
  • Explain quotas and eviction.

Overview

#

Each storage API has a different scope, lifetime, and cost.

Cookies

Small (about 4 KB), sent with matching requests, and readable by the server. Use HttpOnly to hide from JavaScript, Secure for https only, and SameSite to limit cross-site sending. Do not store large data or secrets in cookies.

Web Storage

localStorage and sessionStorage are synchronous, string-only, and scoped to an origin. localStorage persists until cleared; sessionStorage lasts for the tab session. Synchronous access blocks the main thread, so avoid large reads and writes.

IndexedDB

IndexedDB is asynchronous, transactional, and can store structured clones such as objects and blobs. It is the right choice for large or structured client data.

Cache Storage

Cache Storage holds request and response pairs and is used by service workers. It is not a general key-value store.

Quotas and eviction

The origin has a storage quota. The browser can evict data under pressure unless it is persisted. navigator.storage.estimate() reports usage and quota.

Questions to carry forward

  • When should you use IndexedDB over localStorage?
  • How does navigator.storage.persist work?
  • Why are HttpOnly cookies safer for sessions?

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    Which storage should hold a 5 MB offline dataset of structured records?

    Choose the best answer
  2. Exercise 2Explain

    Explain why storing a session token in localStorage is riskier than in an HttpOnly cookie.

    Reveal the reference answer
    reference.text
    localStorage is readable by any JavaScript on the origin, so a cross-site scripting bug can exfiltrate the token. An HttpOnly cookie is not readable by JavaScript, which removes that exfiltration path.

    Authored expected output: An answer linking JavaScript readability to XSS risk.

    The threat is script access. HttpOnly removes script access entirely, while localStorage does not.

Common misconceptions

#
  • localStorage is asynchronous.
  • Cookies can hold megabytes of data.
  • Cache Storage is a general key-value store.

Follow-up practice

#
  • browser-internals-service-workers

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.