JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)
What is web storage
Web storage is an API that provides a mechanism by which browsers can store key/value pairs locally within the user's browser, in a much more intuitive fashion than using cookies. The web storage provides two mechanisms for storing data on the client.
- Local storage: It stores data for current origin with no expiration date.
- Session storage: It stores data for one session and the data is lost when the browser tab is closed.
Practical Code Example
javascript
// Web Storage API: localStorage and sessionStorage
// 1. localStorage (persists until manually cleared, ~5-10MB limit)
localStorage.setItem('theme', 'dark');
console.log(localStorage.getItem('theme')); // 'dark'
// 2. sessionStorage (cleared when browser tab/session closes)
sessionStorage.setItem('currentStep', 'step2');
console.log(sessionStorage.getItem('currentStep')); // 'step2'