How to Use SessionStorage in JavaScript?

Estimated read time 1 min read

SessionStorage is a type of web storage that allows you to store key-value pairs in a web browser. The data stored in session storage is specific to the current session or tab, and is deleted when the user closes the tab or browser window.

Here are the basic steps to use SessionStorage in JavaScript:

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

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