This guide provides a comprehensive overview of the three main browser storage mechanisms: Cookies, localStorage, and sessionStorage. Each has distinct characteristics, use cases, and limitations that developers should understand when building web applications.
Feature | Cookies | localStorage | sessionStorage |
---|---|---|---|
Server Communication | Sent with every HTTP request | Never sent to server | Never sent to server |
Expiration | Can be set explicitly | Never (persistent) | Tab close |
Storage Capacity | ~4KB total | ~5-10MB | ~5-10MB |
Accessibility | Client-side & server-side | Client-side only | Client-side only |
Scope | Domain-specific | Domain-specific | Tab-specific |
Security Options | HttpOnly, Secure, SameSite | None | None |
Cookies are small text files stored by the browser that are primarily designed for server-client communication.
- Automatically sent with every HTTP request to the same domain
- Limited size of approximately 4KB total (all cookies combined for a domain)
- Can set expiration date or session-only (deleted when browser closes)
- Accessible from both client-side and server-side
- Security options including HttpOnly (inaccessible to JavaScript), Secure (HTTPS only), and SameSite flags
// Basic cookie
document.cookie = "username=John";
// Cookie with expiration (1 day)
const date = new Date();
date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = "username=John; expires=" + date.toUTCString() + "; path=/";
// Secure cookie (HTTPS only) with SameSite
document.cookie = "username=John; Secure; SameSite=Strict; path=/";
// Get all cookies as a string
const allCookies = document.cookie;
// Parse cookies into an object
function getCookies() {
const cookies = {};
const cookieString = document.cookie;
if (cookieString === '') {
return cookies;
}
const cookieParts = cookieString.split(';');
for (let i = 0; i < cookieParts.length; i++) {
const cookiePart = cookieParts[i].trim();
const equalsPos = cookiePart.indexOf('=');
if (equalsPos > 0) {
const name = cookiePart.substring(0, equalsPos);
const value = decodeURIComponent(cookiePart.substring(equalsPos + 1));
cookies[name] = value;
}
}
return cookies;
}
// Delete a cookie by setting its expiration to the past
function deleteCookie(name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
}
localStorage provides a way to store key-value pairs in the browser without expiration.
- Never sent to the server
- Larger capacity of approximately 5-10MB (varies by browser)
- No expiration - data persists even after browser is closed
- Available across browser tabs/windows for the same domain
- Client-side only - not accessible from the server
- Simpler API compared to cookies
// Store data
localStorage.setItem('username', 'John');
// Read data
const username = localStorage.getItem('username');
// Remove item
localStorage.removeItem('username');
// Clear all items
localStorage.clear();
// Get number of items
const count = localStorage.length;
// Get key by index
const key = localStorage.key(0); // First key
sessionStorage works similarly to localStorage but with a limited lifetime.
- Never sent to the server
- Larger capacity of approximately 5-10MB (varies by browser)
- Expires when the tab is closed
- Limited to the current tab/window
- Client-side only - not accessible from the server
- Same API as localStorage
// Store data
sessionStorage.setItem('tempData', 'someValue');
// Read data
const tempData = sessionStorage.getItem('tempData');
// Remove item
sessionStorage.removeItem('tempData');
// Clear all items
sessionStorage.clear();
- You need the data on the server
- You need to set security flags like HttpOnly
- You need to ensure compatibility with older browsers
- The data is small and performance impact is acceptable
- You need to store larger amounts of data
- You want data to persist across browser sessions
- You don't need the server to access the data
- You want to avoid performance overhead of sending data with every HTTP request
- You need data to be available only for the current session
- You want to isolate data to a specific tab
- You're storing temporary UI state
- You're concerned about leaving persistent data
- Can be made HttpOnly to prevent XSS attacks
- Can be made Secure to ensure HTTPS-only transmission
- Can implement SameSite to prevent CSRF attacks
- Still vulnerable if these flags aren't set
- Not vulnerable to CSRF attacks
- Vulnerable to XSS attacks (if site has JavaScript vulnerabilities)
- No built-in security features like HttpOnly
- Same-origin policy provides domain isolation
- Don't store sensitive information in any client-side storage without encryption
- Set appropriate cookie flags (HttpOnly, Secure, SameSite) for security
- Keep an eye on storage limits especially with cookies
- Implement data validation before using stored data
- Provide a fallback when storage is disabled or full
- Clear sensitive data when no longer needed
- Consider user privacy regulations like GDPR and ePrivacy Directive
All major browsers include tools for inspecting and managing stored data:
- Chrome: DevTools > Application > Storage
- Firefox: DevTools > Storage
- Safari: DevTools > Storage
- Edge: DevTools > Application > Storage
These tools allow you to view, edit, and delete stored data during development.
Understanding the differences between cookies, localStorage, and sessionStorage is essential for making informed decisions about client-side data storage. By choosing the right storage mechanism for your specific needs, you can optimize for performance, security, and user experience in your web applications.