JavaScript LocalStorage and SessionStorage
Save data in the browser with JavaScript localStorage and sessionStorage. Store objects as JSON, set expiry times, sync tabs and handle storage limits.
10 programs · open one, then the next
Start with LocalStorage Basics- 01LocalStorage BasicsBeginnerLearn localStorage in JavaScript: setItem, getItem, removeItem, clear, keys, storing objects with JSON and checking availability. Explanation and output.Open example →js// Method 1: Set item localStorage.setItem('username', 'John'); localStorage.setItem('age', '30'); localStorage.setItem('isLoggedIn', 'true');
- 02SessionStorage BasicsBeginnerLearn sessionStorage in JavaScript: tab-only storage, page view counts, navigation history, temporary form data and session timeouts. Explanation and output.Open example →js// Method 1: Set item sessionStorage.setItem('sessionId', 'abc123'); sessionStorage.setItem('pageViews', '5'); sessionStorage.setItem('lastPage', '/home');
- 03Storage Event ListenerIntermediateUse the storage event in JavaScript to react to localStorage changes from other tabs, plus custom events in the same tab. Explanation and output.Open example →js// Method 1: Listen to storage changes window.addEventListener('storage', function(e) { console.log('Storage changed'); console.log('Key:', e.key);
- 04Storage JSON HandlingIntermediateStore objects and arrays in localStorage with JSON in JavaScript: stringify, parse, safe defaults, a helper class and expiry times. Explanation and output.Open example →js// Method 1: Store object const user = { id: 1, name: 'John Doe',
- 05Storage Quota ManagementIntermediateManage browser storage quota in JavaScript: estimate(), localStorage size, QuotaExceededError and cleanup of old data, with explanation and output.Open example →js// Method 1: Check storage quota async function getStorageQuota() { if ('storage' in navigator && 'estimate' in navigator.storage) { const estimate = await navigator.storage.estimate();
- 06Storage UtilitiesIntermediateBuild storage utility classes in JavaScript: a JSON helper, expiring items, namespaced keys and an XOR encoding demo, with explanation and output.Open example →js// Method 1: Storage helper class class StorageUtil { constructor(storage = localStorage) { this.storage = storage;
- 07Storage MigrationAdvancedMigrate stored data in JavaScript: rename keys, track versions, back up first and run step-by-step localStorage migrations. Explanation and output.Open example →js// Method 1: Simple migration function migrateStorage() { const oldData = localStorage.getItem('oldKey'); if (oldData) {
- 08Storage Session ManagementIntermediateManage user sessions in JavaScript with sessionStorage and localStorage: create, expire, idle warnings and remember me, with explanation and output.Open example →js// Method 1: Basic session management class SessionManager { constructor() { this.sessionKey = 'userSession';
- 09Storage Custom HooksAdvancedWrite reusable storage hooks in JavaScript like useLocalStorage, with defaults, expiry and cross-tab sync. Includes a fixed setter bug, explanation and output.Open example →js// Method 1: useLocalStorage hook (React-like) function useLocalStorage(key, initialValue) { let storedValue = (function() { try {
- 10Storage Cross-Tab CommunicationAdvancedCommunicate between browser tabs in JavaScript with storage events, BroadcastChannel, shared state and leader election. Simple explanation and real output.Open example →js// Method 1: Basic cross-tab messaging class CrossTabMessenger { constructor(channel) { this.channel = channel;