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<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
- Save a string with
setItem - Reload the page and
getItemagain - Repeat with
sessionStorageand close the tab to compare - 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
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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