What does sessionStorage do?
Definition
sessionStorageis a browser object that stores "key-value" pairs (strings) only while the current tab (or window) is open. Once the tab is closed, the data is automatically removed.
Put simply: it is temporary storage, available only within a single tab and domain.
1. A simple example
// Save data
sessionStorage.setItem('user', 'Tim');
// Read data
console.log(sessionStorage.getItem('user')); // "Tim"
// Remove one item
sessionStorage.removeItem('user');
// Clear the whole storage
sessionStorage.clear();2. Difference from localStorage
| Storage | Lifetime | Shared between tabs | Cleared on close |
|---|---|---|---|
| localStorage | Permanent (until cleared manually) | Yes (between tabs of the same domain) | No |
| sessionStorage | While the tab is open | No (each tab has its own) | Yes |
In other words, if a user opens your site in a new tab,
they get a new, empty sessionStorage.
3. All the methods are the same as localStorage
| Method | What it does | Example |
|---|---|---|
setItem(key, value) | Saves a value | sessionStorage.setItem('token', 'abc') |
getItem(key) | Returns a value | sessionStorage.getItem('token') |
removeItem(key) | Removes an item | sessionStorage.removeItem('token') |
clear() | Clears everything | sessionStorage.clear() |
key(index) | Returns the key at an index | sessionStorage.key(0) |
length | Number of entries | sessionStorage.length |
4. Example: form data kept while the tab is open
<input id="name" placeholder="Name" />
<script>
const input = document.getElementById('name');
// Restore the value on page reload
input.value = sessionStorage.getItem('name') || '';
// Save on change
input.addEventListener('input', () => {
sessionStorage.setItem('name', input.value);
});
</script>Now the entered name survives a page reload, but if you close the tab and open the site again, the field clears.
5. Example: storing data for a single authorization session only
sessionStorage.setItem('sessionToken', 'abc123');
// the token survives a page reload
console.log(sessionStorage.getItem('sessionToken')); // "abc123"
// but disappears when the tab is closedThis is used when you need to store:
- a token or login state only until the window closes,
- temporary data (for example, form or filter state),
- data "drafts" that do not need to persist.
6. Important details
-
Works only with strings, so for objects:
javascriptsessionStorage.setItem('user', JSON.stringify({ name: 'Tim' })); const user = JSON.parse(sessionStorage.getItem('user')); -
Storage capacity is roughly 5 MB.
-
Data is accessible only from the same domain and tab.
-
You can use the
storageevent, but it does not fire in the same window, only between tabs (which is usually irrelevant for sessionStorage).
Summary
| Parameter | localStorage | sessionStorage |
|---|---|---|
| Storage lifetime | Forever (until cleared) | While the tab is open |
| Access between tabs | Yes | No |
| Cleared when the window closes | No | Yes |
| Capacity | ~5-10 MB | ~5 MB |
| Data type | Strings only | Strings only |
| Common use | Settings, themes, user data | Temporary sessions, forms, filters |
Remember it simply:
localStorageis "forever",sessionStorageis "while the tab is open".
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.