Master JavaScript Programming From Scratch

Clear, interactive, and structured JavaScript lessons designed for absolute beginners.

Session Storage

Session Storage allows JavaScript applications to store data temporarily in the browser. The stored data is available during the current browser session.

What is Session Storage?

Session Storage is a browser storage mechanism that stores data as key-value pairs.

Unlike Local Storage, Session Storage is associated with the current browser session. The stored data is normally removed when the browser tab or window is closed.

Simple Definition

Session Storage is used to temporarily store data for the current browser session using key-value pairs.

How Session Storage Works
JavaScript
→
setItem()
→
Session Storage
→
getItem()
JavaScript Session Storage

Session Storage stores temporary data for the current browser session.

Session Storage Methods

setItem() – Stores a value in Session Storage.

getItem() – Retrieves a stored value.

removeItem() – Removes a specific stored value.

clear() – Removes all Session Storage data.

Example 1: Store and Retrieve Data

In this example, we store a user's name and retrieve the value using getItem().

sessionStorage.setItem("username", "Rahul");

var username = sessionStorage.getItem("username");

console.log(username);
Output:
Rahul

Example 2: Remove Data

The removeItem() method removes a specific item from Session Storage.

sessionStorage.setItem("course", "JavaScript");

sessionStorage.removeItem("course");

console.log("Course removed");
Output:
Course removed

Example 3: Clear Session Storage

The clear() method removes all data stored in Session Storage.

sessionStorage.clear();

console.log("Session Storage cleared");
Output:
Session Storage cleared

Important Points

• Session Storage stores data as key-value pairs.

• It is intended for temporary browser-session data.

• Data can be accessed using JavaScript.

• setItem() stores data.

• getItem() retrieves data.

• removeItem() removes one item.

• clear() removes all Session Storage data.

Summary

Session Storage provides a simple way to store temporary data in the browser. The main methods are setItem(), getItem(), removeItem() and clear().