localStorage
A browser feature that saves small text values for a website so they are still there after the page is closed and reopened.
In more detail
A theme toggle stores the user's choice, such as "dark", in localStorage and reapplies it on the next visit. Values are strings, limited to a few megabytes, and readable by any script on the same site, so they must not hold secrets.
Example
In the browser: It holds around five megabytes per origin and is readable by any script on the page, so never store secrets there. Calls are synchronous and throw QuotaExceededError when full.Related terms
- dark mode — A colour theme with light text on dark backgrounds, offered alongside the usual light theme.
- theming — Giving the same components different looks, such as light, dark or a different brand, by swapping the token values they use.
- sessionStorage — Browser storage like localStorage, but scoped to a single tab and cleared when that tab is closed.
- Web Storage API — The simple browser key-value storage made of localStorage and sessionStorage, which stores strings per origin.
- storage quota — The maximum amount of data a browser lets one origin store.
- QuotaExceededError — The error thrown when a write would take an origin over its storage quota.
- HttpOnly — A cookie attribute that stops scripts running in the page from reading the cookie, while the browser still sends it on requests.
- cross-site scripting — An attack in which someone gets their own script to run inside your page, where it can read data, act as the user or steal tokens.
- access token — A generated string that proves who you are to a service, used instead of a password and revocable on its own.
- JSON — A simple text format for structured data made of keys, values, lists and nested objects.
- untrusted input — Any data a program did not create itself, including arguments, form fields, request bodies, file contents, file names and environment variables.
Taught in
- Dark Mode Strategies: System Preference, Class & High-Contrast — Design Systems & Component Engineering
- Client Storage Paradigms: Cookies, Storage Tradeoffs — Frontend Architecture & Web Platform
- Session Architecture: HTTP-Only Cookies vs Tokens — Production Full-Stack Engineering
- any Versus unknown: Defensive Typing In Practice — Production TypeScript