Local Storage and Session Storage in JavaScript

Javascript 9 min min read Updated: Mar 09, 2026 Intermediate
Local Storage and Session Storage in JavaScript
Intermediate Topic 11 of 15

Modern web applications often need to store data on the user's browser so that information can be reused later. JavaScript provides two important storage mechanisms called Local Storage and Session Storage. These features allow developers to store data directly in the browser without using a database.

Both Local Storage and Session Storage are part of the Web Storage API. They store data as key–value pairs and allow websites to save information such as user preferences, login states, and application settings.

What is Web Storage?

Web Storage is a browser feature that allows JavaScript to store data locally in the user's browser. The stored data can be accessed later without needing a server request.

Key Point: Web Storage stores data in the browser as key–value pairs.

Local Storage

Local Storage allows developers to store data permanently in the browser. The stored data remains available even if the user refreshes the page, closes the browser, or restarts the system.

Local Storage is commonly used for saving user preferences, theme settings, and application data.

Storing Data in Local Storage

javascript localStorage.setItem("username", "Rahul");
Output

The value "Rahul" is stored in Local Storage with the key "username".

Retrieving Data from Local Storage

javascript let user = localStorage.getItem("username"); console.log(user);
Output

Rahul

Removing Data from Local Storage

javascript localStorage.removeItem("username");
Output

The stored value with key "username" is removed from Local Storage.

Key Point: Local Storage data persists even after the browser is closed.

Session Storage

Session Storage is similar to Local Storage but with one key difference: the stored data is available only during the browser session. Once the browser tab is closed, the stored data is automatically removed.

Session Storage is commonly used for temporary data such as session information or form data during a user's visit.

Storing Data in Session Storage

javascript sessionStorage.setItem("sessionUser", "Student");
Output

The value "Student" is stored in Session Storage.

Retrieving Data from Session Storage

javascript let sessionUser = sessionStorage.getItem("sessionUser"); console.log(sessionUser);
Output

Student

Difference Between Local Storage and Session Storage

  • Local Storage stores data permanently in the browser.
  • Session Storage stores data only for the current browser session.
  • Local Storage data remains after closing the browser.
  • Session Storage data is removed when the browser tab is closed.
Key Point: Both Local Storage and Session Storage store data in the browser without sending it to the server.

Conclusion

Local Storage and Session Storage are powerful browser features that allow JavaScript to store data locally. They help developers build faster and more interactive web applications by reducing server requests and preserving user data.

Local Storage is suitable for long-term data storage, while Session Storage is ideal for temporary session-based information.

In the next tutorial, you will learn about Cookies in JavaScript, which is another method for storing small pieces of data in the browser.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators