Instruction: Describe the functionalities and differences of `localStorage`, `sessionStorage`, and cookies.
Context: The question tests the candidate's knowledge of client-side storage mechanisms provided by HTML5 and their ability to distinguish between them in terms of scope, lifecycle, and capacity.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
localStorage is a key-value storage that allows us to store data with no expiration date. This means the data stored in localStorage persists even after the browser window is closed. It's perfect for saving user preferences or data that should be kept across sessions. The storage limit is quite generous, usually around 5MB or more depending on the browser. A significant advantage of localStorage is its simplicity and ease of use. However, it's important to note that it's accessible from the same origin only, meaning it cannot be accessed by other domains.
Moving on to sessionStorage, it's quite similar to localStorage in terms of the API but with a key difference in its lifecycle. sessionStorage stores data for one session only, meaning the...