Home / Learn HTML / HTML Web Storage

HTML Web Storage

Save small data inside the visitor browser. It stays even after closing the tab.

In shortlocalStorage stays for ever. sessionStorage is erased when the tab closes.

Definition

Web Storage lets JavaScript keep small amounts of data in the visitor's browser. localStorage stays until deleted; sessionStorage is cleared when the tab closes. Both store text as key and value pairs, about 5 MB per site, and are never sent to the server automatically.

Careful:Do not store passwords or private data in web storage; any script on the page can read it.

Theory in plain words

Two kindslocalStorageStays after closingPer sitesessionStorageCleared with the tabPer tab
Two kinds

What it is

Web Storage lets a page save small data in the browser: localStorage stays, sessionStorage ends with the tab.

Why we use it

It remembers settings such as theme or a draft without a server or cookies.

How it works

Data is saved as text keys and values for one site. Scripts read and write it. It is not safe for secrets.

Where it is used

  • Dark mode setting
  • Shopping cart draft
  • Saved form data
Save and readTry it Yourself
Remember what you typedTry it Yourself

Some browsers block storage inside previews. If it does not remember, try on your own page.

Visit counterTry it Yourself
sessionStorageTry it Yourself
Store an objectTry it Yourself
Remove and clearTry it Yourself
Dark mode that remembersTry it Yourself
List all keysTry it Yourself
Careful:Never store passwords or private data in web storage. Anyone using that browser can read it.

Quick quiz

1. Which storage stays after closing the browser?

2. Storage keeps values as

3. Should you store passwords in it?

Common questions

Which storage stays after closing the browser?

localStorage. localStorage persists.

Should you store passwords in it?

Never. Anyone on that browser can read it.

My notes
Ask a question about this lesson