JavaScript Notes
Master JavaScript sessionStorage for tab-scoped browser storage. Learn setItem, getItem, JSON storage, tab isolation, multi-step form persistence, and key differences from localStorage with examples.
What is sessionStorage?
sessionStorage stores data for the duration of a single browser tab session. The moment the user closes the tab (not just the page — the entire tab), all data is gone. Opening the same URL in a new tab creates a completely separate storage.
Storing Objects and Arrays
Just like localStorage, sessionStorage requires JSON serialization:
"Rahul Sharma"
"Bengaluru"
{ fullName: "Rahul Sharma", email: "rahul@example.com", phone: "...", address: "...", city: "Bengaluru", pincode: "560001" }Real World Use Case: Multi-Step Form Wizard
Tab Isolation Demo
This is the most important behavior of sessionStorage:
// In Tab 1:
sessionStorage.setItem('tabName', 'Tab 1');
console.log(sessionStorage.getItem('tabName')); // "Tab 1"
// In Tab 2 (same URL, opened separately):
console.log(sessionStorage.getItem('tabName')); // null — completely isolated!
sessionStorage.setItem('tabName', 'Tab 2');
// Tab 1 is completely unaffected by Tab 2's changes
// (Unlike localStorage which IS shared between tabs)// Tab 1: "Tab 1" // Tab 2: null
Common Mistakes
❌ Mistake 1: Expecting sessionStorage to persist after tab close
// ❌ Wrong expectation
sessionStorage.setItem('userPref', 'dark');
// User closes tab and reopens the URL...
console.log(sessionStorage.getItem('userPref')); // null! Gone.
// ✅ Use localStorage for persistent preferences
localStorage.setItem('userPref', 'dark');
// Survives tab closes and browser restarts❌ Mistake 2: Expecting data to be shared between tabs
// Tab 1 sets data
sessionStorage.setItem('shared', 'hello');
// ❌ Wrong: Tab 2 expects to see Tab 1's data
console.log(sessionStorage.getItem('shared')); // null! Tabs are isolated.
// ✅ Use localStorage for cross-tab data sharing
localStorage.setItem('shared', 'hello'); // Accessible from all tabs❌ Mistake 3: Not handling missing session data on page refresh
Interview Questions
Q1. What is sessionStorage and how does it differ from localStorage? > Both are Web Storage APIs with the same interface. The key difference: localStorage persists forever (until cleared), while sessionStorage only lasts for the duration of the browser tab. sessionStorage is also NOT shared between tabs — each tab has its own independent storage.
Q2. Does sessionStorage persist through page refreshes? > Yes! A page refresh (F5 or navigation within the same tab) keeps sessionStorage data intact. Only closing the tab destroys the data. This makes it perfect for multi-page form wizards within a single user session.
Q3. What happens to sessionStorage when you duplicate a tab? > When you duplicate a tab (Ctrl+D or right-click → Duplicate), the new tab inherits a copy of the original tab's sessionStorage at that moment. After that, they're independent — changes in one don't affect the other.
Q4. Can two tabs on the same website share sessionStorage? > No. sessionStorage is tab-isolated. Each tab has its own independent sessionStorage. Use localStorage for cross-tab data sharing, or the BroadcastChannel API for real-time cross-tab messaging.
Q5. What is sessionStorage useful for in real applications? > Multi-step form wizards (store intermediate data between steps), temporary search filters within a session, unsaved draft data, shopping cart data for a single checkout flow, and preventing data loss on page refresh within the same tab.
Q6. What storage limit does sessionStorage have? > Typically ~5MB per origin, same as localStorage. It varies slightly between browsers. Use it for small data like form state, not large files or datasets.
Key Takeaways
✅ sessionStorage data lives only for the current TAB's lifetime
✅ Closing the tab destroys all sessionStorage data permanently
✅ Page refresh keeps sessionStorage data — it ONLY dies on tab close
✅ Tab-isolated — two tabs with same URL have SEPARATE sessionStorage
✅ Same API as localStorage: setItem, getItem, removeItem, clear
✅ Strings only — use JSON.stringify/parse for objects
✅ Perfect for multi-step forms and temporary session state
✅ Use localStorage when you need data to persist across tabs/sessionsExam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript sessionStorage - Complete Guide to Session-Based Web Storage.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, dom, session, storage
Related JavaScript Master Course Topics