HTML/CSSIntermediate#storage#javascript

What is the difference between localStorage, sessionStorage, and cookies?

localStorage persists indefinitely (~5-10MB) until cleared, scoped per origin. sessionStorage persists only for the tab session. Cookies are small (~4KB), sent with every HTTP request to the server, and can have expiry dates — used for server-side session tracking.

Example
localStorage.setItem('theme', 'dark');
sessionStorage.setItem('draft', 'hello');
document.cookie = 'user=john; max-age=3600; path=/';

Related Questions

1
HTML/CSSAdvanced#storage#javascript

What is IndexedDB used for?

Open
2
HTML/CSSAdvanced#storage#pwa

What is the Cache API and how does it relate to Service Workers?

Open
3
HTML/CSSIntermediate#css#specificity

What is CSS specificity and how is it calculated?

Open