Skip to main content

Command Palette

Search for a command to run...

Local Storage, Session Storage

Published
•1 min read•View as Markdown
A

As a senior full-stack developer, I am passionate about creating efficient and scalable web applications that enhance the user experience. My expertise in React, Redux, NodeJS, and Laravel has enabled me to lead cross-functional teams and deliver projects that consistently exceed client expectations.

Expert-Level Explanation

Local storage and session storage are web storage solutions for storing key-value pairs in a web browser. Local storage persists data even when the browser is closed and reopened, whereas session storage keeps data for a single session and is cleared when the tab or browser is closed.

Creative Explanation

Think of local storage as a notebook where you jot down reminders (data) that stay even after the day ends. Session storage is like writing on a whiteboard; the notes (data) stay as long as the meeting (session) goes, but once it's over, the board is wiped clean.

Practical Explanation with Code

// Using Local Storage
localStorage.setItem('key', 'value');
console.log(localStorage.getItem('key')); // 'value'

// Using Session Storage
sessionStorage.setItem('sessionKey', 'sessionValue');
console.log(sessionStorage.getItem('sessionKey')); // 'sessionValue'

Real-world Example

Using local storage is like storing information in a filing cabinet at your office; it stays there until you remove it. Session storage is like keeping notes on a pad during a meeting, which you discard after the meeting is over.