LocalStorage Set Get
Predict Output Easy
Question
What does this code print?
Code
localStorage.setItem("key", "value");
console.log(localStorage.getItem("key"));
Hint
setItem stores, getItem retrieves.
Explanation
localStorage.setItem("key", "value") stores the value. localStorage.getItem("key") retrieves it, returning "value". The data persists even after the browser is closed and reopened. Both key and value must be strings; use JSON.stringify for objects. Remove with removeItem or clear.
Related Resources
Related Lessons
- Web Storage
Learn web storage.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.