InterviewHubPRO
Section Overview
Q46

Question 46 of 521

Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

9%
JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

What is the main difference between localStorage and sessionStorage

LocalStorage is the same as SessionStorage but it persists the data even when the browser is closed and reopened(i.e it has no expiration time) whereas in sessionStorage data gets cleared when the page session ends.

Practical Code Example

javascript
// Tab 1:
localStorage.setItem('app_setting', 'v1');
sessionStorage.setItem('tab_session', 'session_tab_1');

// If the user opens Tab 2 on the same domain:
// localStorage.getItem('app_setting') -> 'v1' (SHARED!)
// sessionStorage.getItem('tab_session') -> null (ISOLATED TO TAB 1!)