Local Storage
In short
Local storage is a browser feature that lets a website save text as key-value pairs on the user's device, where it stays even after the browser is closed.
What is local storage?
Local storage, used in JavaScript through the localStorage object, is part of the Web Storage API built into every modern browser. It stores data as key-value pairs, where both keys and values are strings, and the data stays on the device until your code or the user deletes it, even after the browser is closed and reopened. Websites use it to remember things like a chosen theme or language, dismissed banners, and unsaved drafts.
The API is small and synchronous: setItem saves a value, getItem reads it, removeItem deletes one entry, and clear deletes everything. Because only strings are stored, objects are usually converted with JSON.stringify before saving and JSON.parse after reading. The data is scoped to the origin, meaning the combination of protocol, domain, and port, and browsers typically allow about 5 MB per origin.
Its sibling, sessionStorage, has exactly the same API but a shorter memory: its data belongs to a single browser tab and is deleted when that tab is closed. Local storage also differs from cookies. Cookies hold only about 4 KB and are sent to the server automatically with every matching HTTP request, while local storage holds much more, stays in the browser, and is never sent anywhere unless your code sends it.
Think of local storage as a small notebook the browser keeps for each website. Because any JavaScript running on the page can read that notebook, a cross-site scripting (XSS) attack can steal whatever it contains, so it should not hold passwords or session tokens, which are safer in HttpOnly cookies that scripts cannot read. For large or structured data, such as data for an offline app, browsers offer IndexedDB instead.
Key takeaways
- Local storage saves string key-value pairs in the browser, separately for each origin.
- Data persists after the browser closes, until code or the user deletes it.
sessionStoragehas the same API, but its data is cleared when the tab closes.- Unlike cookies, local storage data is not sent to the server with requests.
- Any script on the page can read it, so don't store passwords or session tokens there.
Example
// Save a user preference; values are always stored as strings
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme")); // "dark"
// Store an object by converting it to JSON
const settings = { fontSize: 16, showTips: false };
localStorage.setItem("settings", JSON.stringify(settings));
const saved = JSON.parse(localStorage.getItem("settings") ?? "{}");
// sessionStorage works the same way but is cleared when the tab closes
sessionStorage.setItem("draft", "Hello...");
localStorage.removeItem("theme"); // delete a single keyReaders ask
What is the difference between localStorage and sessionStorage?
Both store string key-value pairs in the browser and share the same API. localStorage data persists until it is deleted, while sessionStorage data belongs to one tab and is cleared when that tab is closed.
What is the difference between local storage and cookies?
Cookies are small (about 4 KB), can have an expiry date, and are sent to the server with every matching request, which makes them suitable for sessions. Local storage holds more data, stays in the browser, and can only be read by JavaScript from the same origin.
Is it safe to store a JWT in local storage?
It is risky, because any script on the page, including one injected through an XSS attack, can read local storage. Many security guides recommend keeping session tokens in HttpOnly, Secure cookies instead, combined with CSRF protection.
See also
- Cookies vs Local StorageCookies are small data the browser sends to the server with every matching request, while local storage keeps larger data in the browser and never sends it.
- localStorage vs sessionStorageBoth store strings by key with the same API; localStorage keeps data for every tab until deleted, while sessionStorage keeps it for one tab until it closes.
- IndexedDB vs localStoragelocalStorage is a small, synchronous store of strings; IndexedDB is an asynchronous browser database for large structured data and files.
- CookieWeb Development, p. 10A cookie is a small piece of data a website asks the browser to store and send back with later requests, often used to keep users logged in.
- Web BrowserWeb Development, p. 67A web browser is an application that fetches web pages from servers and turns their HTML, CSS, and JavaScript into the interactive pages you see on screen.
- JavaScriptWeb Development, p. 29JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- XSSSecurity, p. 55XSS is a vulnerability that lets an attacker inject malicious JavaScript into a trusted website so that it runs in other users' browsers.
- SessionBackend & APIs, p. 47A session is a way for a server to remember a user across many requests, usually by keeping their data on the server and giving the browser a session ID.
- JWTSecurity, p. 23A JWT is a compact, signed token that carries claims like a user ID and expiry time, letting a server verify requests without looking up a session.
- Session StorageWeb Development, p. 49Session storage is a browser feature that saves text as key-value pairs for one tab only; the data survives reloads but is deleted when the tab is closed.
- IndexedDBWeb Development, p. 28IndexedDB is a database built into web browsers that stores large amounts of structured data and files on the user's device, with indexes and transactions.
Free · once a week
Software and AI, explained once a week
The week's software and AI news with every term explained, plus a term to learn and a quick quiz. A two-minute read.
See this week's issueAbout this page
Spotted a mistake or something missing on this page?Suggest an edit
How pages are made