Cookies vs localStorage vs sessionStorage: Choosing Client Storage

Web2026-09-30TryQuickToolBox

When building a web application, you often need to store data on the client side—whether it's a user's theme preference, a shopping cart, or an authentication token. The three main options are cookies, localStorage, and sessionStorage. Each has distinct characteristics that make it suitable for different scenarios. Choosing the wrong one can lead to security vulnerabilities, performance issues, or a poor user experience.

This article breaks down the differences, provides practical guidance, and helps you decide which storage mechanism to use for your next project.

Quick Comparison

Before diving into details, here's a high-level overview of the three storage types:

Feature Cookies localStorage sessionStorage
Capacity ~4KB per domain ~5-10MB per origin ~5-10MB per origin
Persistence Configurable expiry Until explicitly cleared Until tab/window closes
Sent to server Automatically with every HTTP request No No
Accessible via JavaScript Yes (unless HttpOnly) Yes Yes
Scope Domain and path Origin (protocol + domain + port) Origin + tab/window
Vulnerable to XSS Yes (if not HttpOnly) Yes Yes
Vulnerable to CSRF Yes (if used for auth) No No

Cookies: The Original Client Storage

Cookies have been around since the early days of the web. They are small pieces of data (max ~4KB) that the browser stores and automatically sends to the server with every HTTP request to the same domain.

When to Use Cookies

Security Considerations

Cookies are sent automatically, which makes them vulnerable to CSRF if used for authentication without additional protections. Always set the SameSite attribute (Lax or Strict) and consider using CSRF tokens. For sensitive data, use HttpOnly to prevent JavaScript access, reducing XSS impact.

Example of setting a secure cookie in an HTTP response:

Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600

localStorage: Persistent Key-Value Storage

localStorage provides a simple key-value store that persists even after the browser is closed. Data is stored per origin (protocol + domain + port) and is not sent to the server automatically. It's ideal for storing non-sensitive data that should survive page reloads and browser restarts.

When to Use localStorage

Security Considerations

localStorage is accessible via JavaScript, so any XSS vulnerability can expose all stored data. Never store sensitive information like passwords, personal identification numbers, or authentication tokens unless you have robust XSS protections in place. If you must store tokens, consider using the HttpOnly cookie approach instead.

Example of using localStorage:

// Save user preference
localStorage.setItem('theme', 'dark');

// Retrieve preference
const theme = localStorage.getItem('theme');

// Remove item
localStorage.removeItem('theme');

sessionStorage: Per-Tab Storage

sessionStorage is similar to localStorage but with a shorter lifespan: data is cleared when the tab or window is closed. It's scoped to a single tab, so data is not shared between tabs or windows, even for the same origin.

When to Use sessionStorage

Security Considerations

Like localStorage, sessionStorage is vulnerable to XSS. However, its limited lifetime and per-tab scope reduce the window of opportunity for attackers. Still, avoid storing highly sensitive data.

Example of using sessionStorage:

// Save form data
sessionStorage.setItem('formStep1', JSON.stringify({name: 'John'}));

// Retrieve form data
const step1 = JSON.parse(sessionStorage.getItem('formStep1'));

How to Choose: A Decision Guide

Use the following ordered list to guide your decision:

  1. Does the server need to read the data on every request? If yes, use cookies. Example: session IDs.
  2. Does the data need to persist across browser sessions? If yes, use localStorage. Example: user preferences.
  3. Should the data be limited to a single tab? If yes, use sessionStorage. Example: multi-step form data.
  4. Is the data sensitive? Avoid storing it in localStorage or sessionStorage. Use HttpOnly cookies for tokens, and never store passwords.
  5. Is the data large? Cookies are limited to ~4KB; localStorage and sessionStorage offer much more capacity.

Security Best Practices

FAQ

Can I use localStorage for authentication tokens?

It's not recommended because localStorage is accessible via JavaScript, making tokens vulnerable to XSS. Prefer HttpOnly cookies with Secure and SameSite flags.

What happens to sessionStorage when I duplicate a tab?

When you duplicate a tab, the new tab gets a copy of the sessionStorage from the original tab at the moment of duplication. After that, they are independent.

Are cookies sent to the server on every request?

Yes, cookies for the current domain are automatically included in every HTTP request, which can impact performance if you store too much data. Use them sparingly.

Need to quickly format or validate JSON data you're storing? Try our JSON Formatter to beautify and debug your JSON payloads before saving them to client storage.