SyntaxStudy
Sign Up
JavaScript Web Storage APIs Overview
JavaScript Beginner 3 min read

Web Storage APIs Overview

Web Storage

Web Storage provides two mechanisms for storing key/value pairs in the browser: localStorage (persists until cleared) and sessionStorage (cleared when tab closes).

Both store strings only and have a capacity of ~5MB per origin.

Example
// localStorage — persists across sessions
localStorage.setItem("user", "Alice");
console.log(localStorage.getItem("user")); // "Alice"

// sessionStorage — cleared when tab closes
sessionStorage.setItem("tempId", "xyz123");

// Both share the same API
// localStorage.length, .key(), .setItem(), .getItem(), .removeItem(), .clear()
Pro Tip

Never store sensitive data (passwords, tokens, PII) in localStorage — it is accessible to any JavaScript on the page.