Skip to content
IT glossary
FrontendUpdated December 3, 2026

localStorage

A website can use localStorage to save small bits of information, such as your preferred theme, in your browser. The information usually remains available after you close the tab or restart the browser.

Also known as: Web Storage

What localStorage is for

localStorage is part of the browser's Web Storage API. Think of it as a small set of labeled values a site can keep in your browser profile. A news site might remember that you prefer a compact layout without saving that choice to your account. The preference has no built-in expiration, but it isn't a backup: clearing site data removes it, and private browsing generally discards it when the private session ends. It doesn't follow you to another device.

Which pages share it?

Storage is scoped to an origin: the combination of scheme, hostname, and port. In ordinary browsing, https://example.com/news and https://example.com/settings share storage even when they're in different tabs. http://example.com and https://shop.example.com each have a different origin from https://example.com. Unlike cookies, localStorage entries aren't attached to HTTP requests by the browser.

Example: remember a layout choice

function readCompactMode() {
  try {
    return localStorage.getItem('ui:compact') === 'true';
  } catch {
    return false;
  }
}

function saveCompactMode(enabled) {
  try {
    localStorage.setItem('ui:compact', String(enabled));
    return true;
  } catch {
    return false;
  }
}

Keys and values are strings. getItem returns null for a missing key, so this example uses an explicit comparison: the stored string 'false' is truthy in JavaScript. Use removeItem to reset this one preference; clear() would wipe every entry in the storage area. For an object, write JSON with JSON.stringify, then parse it and verify its shape when reading. An old version of your app may have stored a different shape under the same key.

When to choose something else

Treat a failed write as a failure to remember the preference, not a reason to stop the user from changing the layout. Browsers can deny access, and a write can throw QuotaExceededError if storage is full. The API is synchronous, so frequent writes or large payloads can stall the main thread. IndexedDB is a better fit for substantial structured data. For data that should survive a reload but normally disappear when the tab closes, use sessionStorage instead.

Don't store secrets or session tokens in localStorage. JavaScript running in the page's origin can read them, including script injected through XSS. An HttpOnly cookie can't be read by page JavaScript, though a cookie-based session still needs appropriate CSRF defenses.

What interviewers look for

A common follow-up is how to keep two tabs in sync. Listen for the storage event in the other tabs, and update the writing tab's UI directly: it won't get an event for its own write. The event is a notification, not a transaction. If two tabs both read a counter at 10 and write back 11, one increment is lost.