The browser offers a range of places to keep data, each with different persistence rules, size limits, and access characteristics. Picking well between them affects performance, how much state a user keeps across sessions, and whether that state can be shared with other people or machines.
localStorage and sessionStorage
localStorage is the most familiar option. Data survives across sessions, never goes to the server, and is visible to every page on the same protocol and domain. It is capped at roughly 5MB. Its API is reachable as window.localStorage and accepts only UTF-16 strings, so values must be converted before saving.
setItem('key', 'value')getItem('key')removeItem('key')
All three calls are synchronous, which makes the API easy to use but also means it blocks the main thread. That blocking, along with the lack of access from web workers and service workers, is why the Google Chrome team advises against relying on it. Their KV Storage experiment, a proposed replacement, was only a trial and has not gone anywhere yet.
sessionStorage is identical in API; the sole difference is that its contents last only for the current session.
Two representative uses: persisting a dark-theme boolean across reloads, and caching a fetch response — Pokémon names from the PokéAPI — so later visits are faster or work offline. Caching requires JSON.stringify on write and JSON.parse on read, a reminder of the string-only constraint. A third case, remembering which page of results a user was on, exposes a weakness: the value lives only on that machine, so the state cannot be shared.
IndexedDB
IndexedDB is the modern option for structured data, including files and blobs, and it indexes records so queries run efficiently. The trade-off is complexity: databases, tables, and transactions all have to be set up by hand, and the code volume is far higher than with localStorage. The native API is event-based; the examples wrap it in a Promise wrapper, but third-party libraries are the recommended route. localForage is one suggestion, since it exposes the localStorage API but progressively enhances — IndexedDB where supported, localStorage otherwise.
Every database access is asynchronous, so the main thread is never blocked. A connection is opened once with a name (my-db), a schema version (1), and an update function that applies changes between versions, much like migrations. The schema here holds a single object store, preferences — the equivalent of an SQL table. Reads and writes must go through transactions, which is the tedious part.
For plain key–value needs the overhead is hard to justify; localStorage or a helper library is more productive. Application data is where IndexedDB earns its keep: hundreds of megabytes and beyond can be stored, so a full Pokémon dataset can live there, offline and indexed. The pagination case gains nothing from the swap — even here the selected page stays local and unshareable, so IndexedDB is no better a fit for it.
Cookies
Cookies are the only storage on this list that the server also sees. They ride along on every request, whether a page navigation or an Ajax call, which enables shared client–server state and state shared across applications on different subdomains — neither of which the other options can do. The cost of that ubiquity is size: cookies must stay small to keep requests lean.
Cookies store strings only, are concatenated into one semicolon-separated string, and travel in the request's cookie header. Each cookie can carry attributes such as expiration, permitted domains, and permitted pages. They can be manipulated from the client or set server-side.
Storing preferences in a cookie makes sense when the server can act on them — for a theme, a server-rendered app could ship the matching CSS file and trim bundle size — or when preferences must be shared across subdomain apps without a database.
Client-side handling is clumsier than expected. Writing means assigning to document.cookie; the example sets a dark_theme cookie with a max-age attribute so it survives tab closure, plus SameSite and Secure (needed because CodePen runs examples in an iframe, and usually unnecessary elsewhere). Reading means parsing. A cookie string looks like this:
key1=value1;key2=value2;key3=value3
Parsing means splitting on semicolons, finding the entry of the form key1=value1, then splitting on the equals sign and taking the last element. Tedious once, then hidden behind a getCookie helper.
Application data does not belong in cookies: it inflates every request and hurts performance, and the server gains nothing from a stale copy of data it already holds. Pagination is likewise a poor fit, for the same locality reason as before.
URL query strings
The URL is not storage in the strict sense, but it is an excellent carrier for shareable state. Query parameters can rebuild the current view, and search queries and filters are the canonical case: searching flexbox on CSS-Tricks produces https://css-tricks.com/?s=flexbox. A URL can be shared, refreshed for fresh results, or bookmarked.
Only strings fit, and total length is limited, so state must stay compact — long, intimidating URLs help no one.
The query string is available as window.location.search and parses cleanly through the URLSearchParams class, with get to read and set to write. Setting alone is not enough; the URL must also be updated via history.pushState or history.replaceState, depending on the desired behavior.
Preferences do not belong here, because the state would have to be appended to every URL the user visits — impossible to guarantee, for instance when arriving from a Google Search link. Application data is out too, for lack of space; even if it fit, the resulting URL would look suspicious enough to resemble a phishing attempt. Temporary state such as pagination is the sweet spot: the page writes a ?page=x parameter on each click, reads it on load to fetch the right page, and the resulting link can be handed to a friend.
Cache API
The Cache API operates at the network layer, holding requests and their responses. It pairs naturally with service workers: a worker can intercept each request, serve a cached response instead of hitting the server, cut load times, and keep the app usable offline. Although built for service workers, it is now also exposed in window, iframe, and worker contexts. Storage is effectively unbounded — hundreds of megabytes or more — the API is asynchronous, and it is reached through the global caches property. The Google Chrome team maintains a tutorial on caching files with service workers.
Extension storage
Browser extensions get one more option, available as chrome.storage or, with Mozilla's polyfill, browser.storage. A storage permission must be declared in the manifest. Two variants exist with the same API, making switches trivial: local storage stays on the machine, while sync storage follows the Google account to every install of the extension. Both are asynchronous and do not block the main thread like localStorage, and usage resembles localStorage. The Chrome docs cover the implementation details.
Choosing
Following the Chrome team's guidance, IndexedDB should be the default: asynchronous, and roomy enough for any payload. localStorage is discouraged but remains simpler to use. Cookies are the way to share client state with a server, chiefly for authentication. Shareable state such as search pages belongs in the URL query string. Extension authors should look into chrome.storage.



