Loading...

HTML Web Storage

Browsers can remember small pieces of data for your site. Web Storage exposes localStorage and sessionStorage to JavaScript.

Direct answer

From script: localStorage.setItem("theme", "dark") and localStorage.getItem("theme"). localStorage persists until cleared; sessionStorage lasts for the tab session. There is no HTML tag that replaces these APIs — you call them from JS loaded by your page.

📊 Storage kinds

API Lifetime (typical)
localStorage Until the user/site clears it
sessionStorage Until the tab/session ends

Tiny localStorage demo

html
HTML Code
<button id="save" type="button">Remember hi</button>
<script>
document.getElementById("save").onclick = function () {
localStorage.setItem("greet", "hi");
alert(localStorage.getItem("greet"));
};
</script>

Try it

  1. Save a string with setItem
  2. Reload the page and getItem again
  3. Repeat with sessionStorage and close the tab to compare
  4. Never store passwords in Web Storage for real apps

Easy mistakes

  • Treating storage as a secure vault
  • Forgetting values are strings (objects need JSON encoding)
  • Expecting an HTML element named storage

Summary

  • Web Storage is a JS API
  • local vs session lifetimes differ
  • Good for light preferences
  • Not for secrets

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top