Skip to content

localStorage

In one line

localStorage is a browser API that stores key-value pairs persistently on the client, surviving page reloads and browser restarts.

In simple words

localStorage is a simple way to store data in the browser that persists even after the user closes the tab or browser. It stores string key-value pairs and has no expiration — data stays until explicitly removed.

The API is straightforward: localStorage.setItem('key', 'value') to store, localStorage.getItem('key') to retrieve, localStorage.removeItem('key') to delete, and localStorage.clear() to remove everything. Values are always strings, so you must JSON.stringify objects and JSON.parse them back.

localStorage is synchronous, has a size limit (usually 5-10MB per origin), and is shared across all tabs on the same origin. It is great for user preferences, theme settings, and small amounts of cached data. Do not store sensitive data in it — any JavaScript on the page can read it.

Example

javascript
// Store user preference
localStorage.setItem("theme", "dark");

// Retrieve it
const theme = localStorage.getItem("theme"); // "dark"

// Store an object
localStorage.setItem("user", JSON.stringify({ name: "Ada" }));
const user = JSON.parse(localStorage.getItem("user")); // { name: "Ada" }

Storing and retrieving a string and an object in localStorage. Objects must be serialized with JSON.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary