DEV Community

Cover image for πŸ” localStorage vs sessionStorage vs Cookies (Complete Frontend Guide)
Pawar Shivam
Pawar Shivam

Posted on

πŸ” localStorage vs sessionStorage vs Cookies (Complete Frontend Guide)

When building authentication, saving user preferences, or managing sessions, developers often choose between localStorage, sessionStorage, and Cookies.

But the real difference appears in network behavior, security, and persistence.

Let’s break it down.


🧠 Storage Architecture (Simple Diagram)

Browser
β”‚
β”œβ”€β”€ localStorage
β”‚     └── Stored in browser
β”‚     └── NOT sent with network requests
β”‚
β”œβ”€β”€ sessionStorage
β”‚     └── Stored per tab session
β”‚     └── Removed when tab closes
β”‚
└── Cookies
      └── Stored in browser
      └── Automatically sent with HTTP requests
Enter fullscreen mode Exit fullscreen mode

Example HTTP Request:

GET /api/profile
Host: example.com
Cookie: sessionId=abc123
Enter fullscreen mode Exit fullscreen mode

Only cookies travel with network requests automatically.


🟒 localStorage

Stores data without expiration.

πŸ“¦ Size
~5–10MB depending on browser

🌐 Network
Not sent to server automatically

πŸ”’ Security
Accessible via JavaScript β†’ vulnerable to XSS attacks

βœ… Pros
β€’ Large storage
β€’ Persistent data
β€’ Simple API
β€’ Good for UI caching

❌ Cons
β€’ Not secure for sensitive data
β€’ Vulnerable to XSS
β€’ Server cannot read it directly

πŸ“Œ Use Cases
β€’ Theme preference
β€’ UI settings
β€’ Cached API data


🟑 sessionStorage

Same API as localStorage but data exists only for one browser tab session.

πŸ“¦ Size
~5MB

🌐 Network
Not sent to server

πŸ”’ Security
Also accessible via JavaScript

βœ… Pros
β€’ Temporary storage
β€’ Tab specific data
β€’ Auto cleared

❌ Cons
β€’ Lost when tab closes
β€’ Not suitable for persistence

πŸ“Œ Use Cases
β€’ Multi-step forms
β€’ Temporary user state


πŸ”΅ Cookies

Small data stored in browser and automatically included in HTTP requests.

πŸ“¦ Size
~4KB

🌐 Network
Sent with every request to the same domain

Example:

Cookie: sessionId=abc123
Enter fullscreen mode Exit fullscreen mode

πŸ”’ Security Options

β€’ HttpOnly β†’ JS cannot access
β€’ Secure β†’ only HTTPS
β€’ SameSite β†’ protects from CSRF

βœ… Pros
β€’ Server can read automatically
β€’ Secure options available
β€’ Best for authentication

❌ Cons
β€’ Small storage
β€’ Sent with every request (network overhead)

πŸ“Œ Use Cases
β€’ Authentication sessions
β€’ Login state
β€’ User tracking


⚑ Real Authentication Flow Example

Modern apps usually use this pattern:

User Login
     ↓
Server verifies credentials
     ↓
Server sets cookie
Set-Cookie: sessionId=xyz; HttpOnly; Secure
     ↓
Browser stores cookie
     ↓
Every API request automatically sends cookie
Enter fullscreen mode Exit fullscreen mode

Example request:

GET /api/user
Cookie: sessionId=xyz
Enter fullscreen mode Exit fullscreen mode

Server reads cookie β†’ authenticates user.


⚑ Quick Comparison

Feature localStorage sessionStorage Cookies
Size ~5–10MB ~5MB ~4KB
Expiration Never Tab close Configurable
Network Requests ❌ No ❌ No βœ… Yes
Accessible by JS βœ… Yes βœ… Yes Optional
Best For UI data Temp data Authentication

πŸ’‘ Security Tip

Avoid storing JWT tokens in localStorage in production apps.

Better approach:

HttpOnly + Secure Cookies
Enter fullscreen mode Exit fullscreen mode

This prevents JavaScript access β†’ protects from XSS attacks.


🎯 Interview Questions Developers Get

β€’ Difference between localStorage and cookies
β€’ Why cookies are used for authentication
β€’ What is HttpOnly cookie
β€’ How cookies prevent CSRF attacks
β€’ Storage limits of browser storage


Small frontend decisions like this can impact security, performance, and scalability of your application.

Which one do you prefer for authentication?

Cookies πŸͺ or localStorage? πŸ€”

Top comments (0)