localStorage vs sessionStorage — JS Technical Interview Questions & Answers

Side-by-side comparison table.

localStorage vs sessionStorage

  • localStorage vs sessionStorage (diff) is a JS topic. In plain words you use it for remember theme in the browser. Don’t start with a slogan — start with that picture.
  • Smallest example: localStorage.setItem('theme', 'dark'). Type it, run it, and say what you see. If you can do that from memory, you know localStorage vs sessionStorage (diff).
  • From the example next to this theory: Side-by-side comparison table.
  • Trap: storing passwords in localStorage. Fix that before you talk about advanced JS.
  • Viva: what is localStorage vs sessionStorage (diff)? Then show localStorage.setItem('theme', 'dark'). Then name the trap.

Technical Interview Tip: What is localStorage vs sessionStorage (diff)? Show this: localStorage.setItem('theme', 'dark'). Trap: storing passwords in localStorage.

Code Examples for localStorage vs sessionStorage

localStorage vs sessionStorage — working example

// Browser:
// document.cookie = "theme=dark; Max-Age=86400; Path=/; SameSite=Lax";
// localStorage.setItem("theme", "dark");
// sessionStorage.setItem("draft", "hello");

// Runnable stand-in:
const jar = { theme: "dark" };
const store = new Map([["theme", "dark"]]);
console.log(jar.theme, store.get("theme"));
store.delete("theme");
console.log(store.has("theme"));

localStorage vs sessionStorage — cookies go to the server; localStorage persists; sessionStorage lasts for the tab.

How to Explain localStorage vs sessionStorage to an Interviewer

What is localStorage vs sessionStorage (diff)?

Used for remember theme the browser Not just the heading

Give one small example of localStorage vs sessionStorage (diff).

localStorage.setItem('theme', 'dark') Side-by-side comparison table

What mistake do beginners make with localStorage vs sessionStorage (diff)?

storing passwords in localStorage Show the fix

Where do you use localStorage vs sessionStorage (diff)?

the browser remember theme