What is localStorage? | Glossary

Glossary

What is localStorage?

localStorage is a browser feature that lets a website save small pieces of text on your device, so settings or a draft survive closing the tab.

localStorage is a small storage area inside the web browser where a website can save text on the visitor's own device. The data stays after the tab or browser is closed, so a site can remember things like a chosen language, dark mode or an unfinished form without a server or an account.

How localStorage works

It is part of the Web Storage API that every modern browser supports. Data is stored as key and value pairs, and both are always strings. In JavaScript you call localStorage.setItem('theme', 'dark') to save, localStorage.getItem('theme') to read, and removeItem or clear to delete. To store an object you convert it with JSON.stringify and read it back with JSON.parse.

Storage is separated by origin, meaning the exact combination of protocol, domain and port. A page on one site cannot read another site's data. Browsers typically allow around 5 MB per origin, and the calls are synchronous, so large reads can briefly freeze the page.

localStorage, sessionStorage and cookies

  • sessionStorage uses the same methods but is wiped when the tab closes.
  • Cookies are much smaller and are sent to the server with every request, which is why logins traditionally use them.
  • localStorage never leaves the browser on its own; only your page's scripts can read it.

For larger or structured data, such as an offline product catalogue, IndexedDB is the better browser database.

Saving a half-finished cake order

A bakery's online order form is long: cake size, flavour, message, pickup date. The page saves each field to localStorage as the customer types. If they close the tab to check their calendar and come back, the form refills itself. Once the order is submitted, the page clears those keys.

When it matters on a small-business site

You rarely touch localStorage directly, but site tools and plugins use it for cart contents, cookie-banner choices and preferences. It matters when something seems "stuck" for one visitor: stale saved data is a common cause, and clearing site data in the browser fixes it.

Mistakes developers make with localStorage

  • Storing login tokens or personal data. Any script on the page, including an injected one, can read it. Session tokens such as a JWT are safer in HttpOnly cookies.
  • Treating it as permanent. Users clear it, private windows discard it, and some browsers evict data from sites not visited for a while.
  • Not handling errors. Writes can throw when storage is full or blocked, so wrap them in try and catch.
  • Forgetting everything is a string. Saving false and reading back "false" is truthy.

Related terms

  • Cookie: smaller storage that travels to the server.
  • Browser cache: where the browser keeps downloaded files, separate from localStorage.
  • Web app: the kind of site that leans on browser storage most.
  • JavaScript: the only way to read or write it.

Ready to put this into practice?

We.Inc lets you describe a site in chat and watch AI build it live, then refine it visually or in code.

Start Building

No credit card required

Describe your business. Get a website ready to publish.

Ready to simplify everything?

Join 15,000+ businesses that replaced their entire marketing stack with one platform.

No credit card required · Talk to sales

Product

Who It's For

Features

Resources

Company

View Sitemap