DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Browser Storage: localStorage, sessionStorage, Cookies, and IndexedDB Compared
XLinkedInReddit
MediumFrontend Engineering

Browser Storage: localStorage, sessionStorage, Cookies, and IndexedDB Compared

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • Quick Comparison
  • localStorage
  • sessionStorage
  • Cookies
  • Security Considerations
  • Decision Guide

Choosing the right storage mechanism depends on your use case. Here's the definitive comparison.

Quick Comparison

FeaturelocalStoragesessionStorageCookiesIndexedDB
Capacity5-10MB5-10MB4KB per cookieGBs
ExpiryNeverTab closeConfigurableNever
Server accessNoNoYes (auto-sent)No
APISyncSyncSync (string)Async
Data typeStringsStringsStringsAny structured
Web WorkersNoNoNoYes

localStorage

// Simple key-value store
localStorage.setItem("theme", "dark");
localStorage.getItem("theme"); // "dark"
localStorage.removeItem("theme");

// Objects need serialization
localStorage.setItem("user", JSON.stringify({ name: "Rahul" }));
const user = JSON.parse(localStorage.getItem("user"));

Best for: User preferences, theme, non-sensitive cached data.

sessionStorage

// Same API as localStorage, but data cleared on tab close
sessionStorage.setItem("scrollPosition", "500");
// Unique per tab — opening same URL in new tab gets fresh storage

Best for: Form state preservation, one-time notifications, tab-specific data.

Cookies

// Setting cookies
document.cookie = "theme=dark; max-age=31536000; path=/; SameSite=Strict";

// HttpOnly cookies (set by server only)
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Strict; Path=/

// Reading cookies (painful API)
const theme = document.cookie
  .split("; ")
  .find(c => c.startsWith("theme="))
  ?.split("=")[1];

Best for: Authentication tokens (HttpOnly), server-read preferences.

Security Considerations

  • localStorage/sessionStorage: Accessible via XSS. Never store auth tokens here.
  • HttpOnly cookies: Not accessible via JavaScript. Best for auth tokens.
  • Secure flag: Only sent over HTTPS.
  • SameSite: Prevents CSRF. Use Strict or Lax.

Decision Guide

  • Auth tokens → HttpOnly cookies
  • User preferences → localStorage
  • Form drafts → sessionStorage
  • Large datasets/offline data → IndexedDB
  • Data the server needs → Cookies

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Quick Comparison
  • localStorage
  • sessionStorage
  • Cookies
  • Security Considerations
  • Decision Guide

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.