local

Local Storage and Session Storage

Local Storage and Session Storage

Web Storage allows you to store data in the user's browser. Local Storage persists until explicitly deleted, while Session Storage is cleared when the browser is closed.

Using Local Storage

Local Storage can be used to store simple data as key-value pairs:

      
        // Store data
        localStorage.setItem("name", "John");

        // Retrieve data
        let name = localStorage.getItem("name");
        console.log(name); // Outputs: John

        // Remove data
        localStorage.removeItem("name");

        // Clear all data
        localStorage.clear();
      
    

Activity

Try It Yourself!

Store your name and age in Local Storage and retrieve it to display on the page.

Quiz

Quick Quiz

  1. What is the difference between Local Storage and Session Storage?
  2. How do you store data in Local Storage?
  3. What is the purpose of localStorage.clear()?

Answers: Local Storage persists, Session Storage does not; use setItem(); clears all stored data.