Local Storage & Session Storage

Learn client-side data storage options for web applications.

Present State

Local Storage and Session Storage are part of the Web Storage API, providing a simple and efficient way to store key-value data on the client side. Local Storage retains data with no expiration date, while Session Storage keeps data only for the duration of the page session.

Code Example

Example demonstrating Local Storage and Session Storage usage:

Local Storage & Session Storage Example

// Save data to Local Storage
localStorage.setItem('username', 'BashaRam');

// Retrieve data from Local Storage
const user = localStorage.getItem('username');
console.log('Local Storage username:', user);

// Remove data from Local Storage
localStorage.removeItem('username');

// Save data to Session Storage
sessionStorage.setItem('sessionID', 'abc123');

// Retrieve data from Session Storage
const session = sessionStorage.getItem('sessionID');
console.log('Session Storage sessionID:', session);

// Clear all Session Storage data
sessionStorage.clear();
          

This code demonstrates:

  • How to store, retrieve, and remove data in Local Storage.
  • How to store, retrieve, and clear data in Session Storage.
  • The difference in data persistence between Local and Session Storage.
  • Simple API usage for client-side storage without server interaction.

Description

Local Storage: Stores data with no expiration time. Data persists even after the browser is closed and reopened. It is useful for saving user preferences or app state between sessions.

Session Storage: Stores data only for the duration of the page session. Data is cleared when the page session ends (e.g., tab or browser is closed). Suitable for temporary data needed during a browsing session.

Both use key-value pairs and store data as strings. They offer a synchronous API, making them easy to use but not suitable for large amounts of data or complex data structures without serialization.

Applications

  • Persisting user preferences like theme, language, or layout settings.
  • Storing temporary form data during a session to prevent data loss.
  • Keeping session identifiers or tokens on the client side.
  • Building offline-capable web apps by caching data locally.
  • Enhancing user experience by reducing server calls for repeated data.

Multiple Choice Questions

Q1: Which of the following statements about Local Storage is true?

  • A. Data is cleared when the browser or tab is closed.
  • B. Data persists even after the browser is closed and reopened.
  • C. It can store complex JavaScript objects without serialization.
  • D. It is only accessible on the server side.

Answer: B. Data persists even after the browser is closed and reopened.

Q2: When does Session Storage data get cleared?

  • A. When the user refreshes the page
  • B. When the user closes the tab or browser window
  • C. After 24 hours automatically
  • D. Never, unless manually cleared

Answer: B. When the user closes the tab or browser window

Q3: How is data stored in Local and Session Storage?

  • A. As raw binary data
  • B. As JSON objects
  • C. As string key-value pairs
  • D. As XML

Answer: C. As string key-value pairs

Q4: Which Web Storage API would you use to store user preferences that should remain after the browser is closed?

  • A. Session Storage
  • B. Local Storage
  • C. Cookies
  • D. IndexedDB

Answer: B. Local Storage