Web applications frequently need to remember things between page loads, between tabs, or across visits — a user's chosen theme, their favourite team, a half-completed form, an authentication session. The browser provides several client-side storage mechanisms for exactly this, and choosing correctly among them is the focus of this lesson. The three classic mechanisms are localStorage, sessionStorage, and cookies. They look superficially similar — all store data in the browser — but they differ in lifetime, capacity, scope, and crucially in whether the data is sent to the server, and those differences determine which is appropriate for a given task. localStorage and sessionStorage share an identical, very simple key-value API and differ only in lifetime: localStorage persists until explicitly cleared, surviving tab and browser closes, while sessionStorage lasts only for the lifetime of a single tab. Both are purely client-side and are never automatically sent anywhere. Cookies are older and fundamentally different in one respect: the browser attaches them to every HTTP request to the matching server automatically. This makes them the natural home for data the server needs on each request — session and authentication tokens above all — but also makes them small, capacity-limited, and a performance consideration since they travel on every request. Getting these choices right has real consequences for correctness, performance, and security. Storing an authentication token in the wrong place opens a security hole; storing large data in cookies bloats every request; storing per-tab state in localStorage causes tabs to interfere with one another. Understanding the trade-offs is what makes the choice deliberate rather than accidental.
25 minbeginner
LocalStorage, SessionStorage and cookies
Analogy🏏Cricket
🏏 Think of it like cricket: Imagine you are following an India vs Australia match but you cannot watch it live — you ask a friend at the Wankhede Stadium to text you the final scorecard. Asking is the asynchronous operation: you do not stand frozen by your phone until the match ends. You go about your day (other code keeps running) and deal with the score when the text arrives. The promise your friend makes — 'I will send you the scorecard when the innings ends' — is exactly a JavaScript Promise: a commitment to deliver a value later. Just as your friend's promise is initially pending (innings in progress), then settles into either fulfilled (they text you '187/4') or rejected (they text 'rain stopped play, no result'), a Promise transitions once from pending to fulfilled or rejected and never flips back. Just as you plan in advance what you will do when the score arrives ('if India scored 180+, I will celebrate; if the feed fails, I will check another source'), `.then()` and `.catch()` register what happens on success and failure. The insight this reveals is that asynchronous programming is not about doing things faster — it is about not standing idle while you wait, so the single JavaScript thread stays free to handle everything else going on.
Lesson 10 of 36
0% complete