Skip to content

LearnWeb-stack/JavaScript-Cookies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Web Storage Guide: Cookies, localStorage, and sessionStorage

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.

Comparison at a Glance

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

Cookies are small text files stored by the browser that are primarily designed for server-client communication.

Key Characteristics

  • 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

Cookie Creation in JavaScript

// 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=/";

Reading Cookies

// 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;
}

Deleting 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

localStorage provides a way to store key-value pairs in the browser without expiration.

Key Characteristics

  • 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

Using localStorage

// 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

sessionStorage works similarly to localStorage but with a limited lifetime.

Key Characteristics

  • 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

Using sessionStorage

// Store data
sessionStorage.setItem('tempData', 'someValue');

// Read data
const tempData = sessionStorage.getItem('tempData');

// Remove item
sessionStorage.removeItem('tempData');

// Clear all items
sessionStorage.clear();

When to Use Each Storage Method

Use Cookies When:

  • 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

Use localStorage When:

  • 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

Use sessionStorage When:

  • 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

Security Considerations

Cookies:

  • 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

localStorage and sessionStorage:

  • 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

Best Practices

  1. Don't store sensitive information in any client-side storage without encryption
  2. Set appropriate cookie flags (HttpOnly, Secure, SameSite) for security
  3. Keep an eye on storage limits especially with cookies
  4. Implement data validation before using stored data
  5. Provide a fallback when storage is disabled or full
  6. Clear sensitive data when no longer needed
  7. Consider user privacy regulations like GDPR and ePrivacy Directive

Debugging Tools

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.

Conclusion

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.

JavaScript-Cookies

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages