Advertisement
Beginner JavaScript, Browser Storage & Web APIs

What's the difference between localStorage and sessionStorage?

Short 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.

Save JavaScript mock interview
Advertisement
AModel 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.
Likely follow-ups
  • Can other tabs read another tab's sessionStorage?
Related topics