Middle+
What is the difference between Session Storage and Local Storage?
sobes.tech AI
Answer from AI
Session Storage and Local Storage are two types of web storage that allow data to be saved on the client side, but they differ in lifespan and scope:
- Session Storage stores data only within a single browser session. When the tab or browser window is closed, the data is deleted.
- Local Storage saves data without time limitations; it remains even after closing and reopening the browser unless explicitly deleted.
Both storages operate on the same principle: they store key-value pairs as strings and are accessible via JavaScript.
Example usage in a browser:
// Write to localStorage
localStorage.setItem('key', 'value');
// Write to sessionStorage
sessionStorage.setItem('key', 'value');
// Retrieve from localStorage
const val = localStorage.getItem('key');
In Node.js, these APIs are not available directly, but there are libraries that emulate their behavior.