LocalStorage — Complete Guide
LocalStorage — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of JavaScript Tutorial on Toolliyo Academy.
On this page
JavaScript Tutorial · Lesson 55 of 100
LocalStorage
Basics ✓ → Objects & data ✓ → Async & DOM ✓ → Advanced → Tools → Projects
Advanced · 4 — Production skills · ~10 min · JS HTML DOM & Web APIs
What is this?
localStorage saves string key-value pairs in the browser — they survive page reloads and browser restarts (same origin).
Why should you care?
Remember theme, language, or draft form text on the client.
See it live — copy this example
Paste into an HTML file or the browser console (F12). Use Run below when the live editor is available.
localStorage.setItem("theme", "dark");
console.log(localStorage.getItem("theme"));
localStorage.removeItem("theme");
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- Only strings — use JSON.stringify for objects.
- Storage is per website origin.
Practice next
- setItem theme dark and getItem.
- Save object with JSON.stringify and parse back.
- Try sessionStorage for tab-only data.
- Persist cart array across reload with JSON parse/stringify wrapper.
- Store lastVisitedRoute and redirect on next visit.
Remember
setItem/getItem/removeItem Strings only — JSON for objects Never store secrets
ScriptVerse theme toggle
Dark mode preference survives reload via localStorage theme key read on boot.
Outcome: Client persistence improves UX for preferences that are not security-sensitive.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!