Predict sessionStorage Behavior
Predict Output Easy
Question
After setting sessionStorage.setItem('key', 'value'), the user closes the tab and reopens the page. What does getItem('key') return?
Code
// Tab 1
sessionStorage.setItem("key", "value");
// User closes tab, reopens the same URL
// Tab 2
console.log(sessionStorage.getItem("key"));
Hint
sessionStorage is scoped to the tab and cleared when it closes.
Explanation
sessionStorage is cleared when the tab (or browser) is closed. When the user reopens the page in a new tab, the sessionStorage is empty, so getItem('key') returns null. If you need data to persist across sessions, use localStorage instead.
Related Resources
Related Lessons
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.