Intermediate Topic 22 of 51 Web Development
Local Storage and Cookies
Browser mechanisms for persisting small data.
How it works
Local storage and session storage are browser key-value stores for strings, with local storage persisting until cleared and session storage lasting for the tab. Cookies are small pieces of data the server sets via headers and the browser sends back with each matching request, with attributes such as Expires, Path, Domain, Secure, HttpOnly and SameSite. IndexedDB offers larger structured storage.
Step-by-step walkthrough
- localStorage keeps strings across sessions.
- sessionStorage lasts until the tab closes.
- Cookies are tiny and sent with every request, ideal for sessions.
- Use HttpOnly and Secure flags for security.
- IndexedDB stores larger structured data.
Where it is used
Remembering preferences, themes, shopping carts, drafts and login sessions use these mechanisms: cookies for server-visible sessions, storage for client-only state.
Common pitfall
Storage is synchronous, limited to a few megabytes and readable by any script on the origin, so tokens stored there are exposed to cross-site scripting; use HttpOnly cookies for session credentials. Cookies add overhead to every request. Users can clear data, so treat storage as a cache. Never store sensitive data unencrypted, and handle exceptions when storage is disabled or full.
Where it fits in the course
Local Storage and Cookies is topic 22 of 51 in Web Development, rated Intermediate. It follows ES Modules. The next topic is CSS Animations.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Local Storage and Cookies?
Where do people use Local Storage and Cookies?
What is the most common mistake with Local Storage and Cookies?
What should I learn before this topic?
How can I practice it?
Is Local Storage and Cookies hard to learn?
How long does it take to learn Local Storage and Cookies?
Which topics are related to Local Storage and Cookies?
What should I study after Local Storage and Cookies?
Related topics
- ES Modules (Web Development)
- CSS Animations (Web Development)
- Event Loop (Web Development)
- Web Accessibility (Web Development)
- Mobile Storage (Mobile, Embedded & IoT)
- Object Storage (Distributed Systems & Cloud)
- Storage Devices (Computer Architecture & Logic)