JavaScript
Session Storage



What is Session Storage?

Session Storage is a web storage API provided by the browser that allows you to store key-value pairs in a user's browser memory for the duration of a page session. The data persists only as long as the browser tab is open.

It is useful for temporary data that you don't want to persist across multiple sessions. Once the user closes the tab or window, the session storage data is cleared automatically.

Key Features of Session Storage

Setting Data in Session Storage

You can store data using sessionStorage.setItem() method, which takes two parameters: key and value.


// Store a user's name
sessionStorage.setItem("username", "JohnDoe");
    

Output:

undefined

Try checking this by typing sessionStorage.getItem("username") in the browser console.

Retrieving Data from Session Storage

Use the sessionStorage.getItem() method to retrieve stored data.


// Retrieve the stored username
const user = sessionStorage.getItem("username");
console.log(user);
    

Output:

JohnDoe

Removing a Specific Item

To remove a particular item, use sessionStorage.removeItem().


// Remove the username from session storage
sessionStorage.removeItem("username");
    

Output:

undefined

Clearing All Session Storage Data

To clear all data stored in session storage, use sessionStorage.clear().


// Remove all session storage data
sessionStorage.clear();
    

Output:

undefined

Example: Counting Page Views in a Tab

Let’s look at a practical example. Suppose you want to count how many times a user has clicked a button during a session.


// Initialize count if not present
if (!sessionStorage.getItem("clickCount")) {
  sessionStorage.setItem("clickCount", "0");
}

// Simulate a click event
function countClick() {
  let count = parseInt(sessionStorage.getItem("clickCount"));
  count++;
  sessionStorage.setItem("clickCount", count.toString());
  console.log("Click count this session:", count);
}

// Call the function a few times
countClick();
countClick();
    

Output:

Click count this session: 1
Click count this session: 2

Common Questions

What happens if I open the same website in another tab?

Session Storage is tab-specific. The new tab has a fresh session with no stored data from the previous tab.

Can I store objects in Session Storage?

Session Storage only stores strings. You can store objects using JSON.stringify() and retrieve them using JSON.parse().


// Storing an object
const user = { name: "Alice", age: 25 };
sessionStorage.setItem("userData", JSON.stringify(user));

// Retrieving the object
const data = JSON.parse(sessionStorage.getItem("userData"));
console.log(data.name);  // Output: Alice
    

When Should You Use Session Storage?

Summary



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M