How to Use Session Storage in JavaScript?

Estimated read time 1 min read

Session storage is a type of web storage that allows you to store key-value pairs in a web browser and maintain their values across multiple browser tabs or windows. The data stored in session storage is specific to the current session and is deleted when the user closes the tab or browser window.

Here are the basic steps to use session storage in JavaScript:

  1. Store data in session storage:
sessionStorage.setItem("key", "value");
  1. Retrieve data from session storage:
var data = sessionStorage.getItem("key");
  1. Remove data from session storage:
  1. Clear all data from session storage:

It’s important to note that data stored in session storage can only be accessed by scripts on the same origin as the data was stored. The maximum size of the data that can be stored in session storage varies between browsers, but is usually around 5-10 MB.

You can also use the length property to get the number of key-value pairs stored in session storage, and the key() method to retrieve the key at a specific index in the storage.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply