JavaScript › Browser Storage & Web APIs

What's the difference between localStorage and sessionStorage?

Beginner

Answer

localStorage persists data with no expiration until explicitly cleared, and is shared across all tabs/windows for the same origin. sessionStorage only lasts for the lifetime of a single tab and is cleared when that tab closes.
localStorage.setItem('theme', 'dark');
sessionStorage.setItem('draftId', '42');
Real-world example localStorage for remembering a user's theme preference; sessionStorage for a temporary multi-step form draft.

Common follow-ups: Can other tabs read another tab's sessionStorage?