Skip to content

JavaScript Tutorial Browser Storage

localStorage and sessionStorage

Lesson 135 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

Web Storage lets a page keep small amounts of data in the visitor’s browser. Two versions exist, and the API is identical.

localStoragesessionStorage
Lifetimestays until deletedcleared when the tab is closed
Shared between tabsyes (same site)no, each tab has its own
Typical usesaved settings, drafts, themetemporary state in one visit

Each is tied to the origin (protocol + domain + port). https://example.com cannot read the data of https://other.com.

script.js
JS
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme")); // dark
script.js
JS
// In this editor, storage lives in memory for each run, so it starts empty each time.
// On a normal page, localStorage keeps its data between visits.
localStorage.setItem("visits", "1");
console.log(localStorage.getItem("visits")); // → 1

What it stores

Only strings. Everything you save is converted to text, and you convert it back when you read it.

script.js
JS
localStorage.setItem("count", 5);
console.log(typeof localStorage.getItem("count")); // → string
console.log(Number(localStorage.getItem("count")) + 1); // → 6

Objects and arrays need JSON, see JSON with localStorage.

Typical uses

  • A chosen theme (light or dark).
  • Form drafts, so a refresh does not lose a long text.
  • A “seen the welcome message” flag.
  • A to-do list in a small app (see the Notes project).

Synchronous

Reading and writing are synchronous, which means they block the page while they run. Fine for small values, but not for large data.

Where to see it

In the browser’s developer tools, open the Application tab (Chrome, Edge) or Storage tab (Firefox) and look under Local Storage. You can view, edit and delete values by hand.

info
In this course's editor
The Try it sandbox is isolated from the real page and from real browser storage. It provides an in-memory version of localStorage for each run, so the code behaves as expected but nothing is saved between runs. In your own pages it is persistent.