๐Ÿ—‚๏ธ Session Storage in JavaScript โ€“ Complete Tutorial

Session Storage is a Web Storage API that allows you to store data as key-value pairs in the browser. Unlike Local Storage, the data exists only for the current browser tab or window and is automatically removed when the tab is closed.

๐Ÿ“Œ What is Session Storage?

Session Storage is accessed using the sessionStorage object. It is useful for temporarily storing data needed during a user's current browsing session.

>>"Session Storage stores temporary data that lasts only for the current browser tab."

๐Ÿ’ก Why Use Session Storage?

  • ๐Ÿ“ Save form data while filling a page
  • ๐Ÿ›’ Store temporary shopping cart information
  • ๐Ÿ” Preserve search filters during navigation
  • ๐Ÿ“„ Store multi-step form progress
  • โšก Keep temporary application state

๐Ÿ›  Session Storage Methods

MethodDescription
setItem()Stores a value.
getItem()Retrieves a value.
removeItem()Removes a specific item.
clear()Removes all stored items.
key()Returns the key at a given index.
lengthReturns the number of stored items.

๐Ÿ“ Storing Data

Use setItem() to save a value.

Store a Value

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

๐Ÿ“– Reading Data

Read a Value

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

console.log(username);

โœ๏ธ Updating Data

Updating is done by storing a new value using the same key.

Update a Value

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

๐Ÿ—‘๏ธ Removing Data

Remove a Value

sessionStorage.removeItem("username");

๐Ÿงน Clearing All Data

Clear Session Storage

sessionStorage.clear();

๐Ÿ“Š Checking the Number of Items

Length Property

console.log(sessionStorage.length);

๐Ÿ”‘ Accessing Keys

Get Key

console.log(sessionStorage.key(0));

๐Ÿ“ฆ Storing Objects

Session Storage stores only strings. Convert objects into JSON before saving.

Store an Object

const user = {
  name: "John",
  age: 25,
  city: "New York"
};

sessionStorage.setItem(
  "user",
  JSON.stringify(user)
);

๐Ÿ“ฅ Retrieving Objects

Retrieve an Object

const user = JSON.parse(
  sessionStorage.getItem("user")
);

console.log(user.name);
console.log(user.city);

๐Ÿ“‹ Storing Arrays

Store an Array

const colors = ["Red", "Green", "Blue"];

sessionStorage.setItem(
  "colors",
  JSON.stringify(colors)
);

๐Ÿ“ค Reading Arrays

Retrieve an Array

const colors = JSON.parse(
  sessionStorage.getItem("colors")
);

console.log(colors);

๐Ÿ›  Helper Functions

Save Data

saveData()

function saveData(key, value) {
  sessionStorage.setItem(
    key,
    JSON.stringify(value)
  );
}

Load Data

loadData()

function loadData(key) {
  const data = sessionStorage.getItem(key);

  return data
    ? JSON.parse(data)
    : null;
}

Delete Data

deleteData()

function deleteData(key) {
  sessionStorage.removeItem(key);
}

๐Ÿš€ Complete Example

Temporary User Session

const session = {
  username: "John",
  currentPage: "Dashboard",
  lastVisited: Date.now()
};

// Save
sessionStorage.setItem(
  "session",
  JSON.stringify(session)
);

// Read
const currentSession = JSON.parse(
  sessionStorage.getItem("session")
);

console.log(currentSession);

// Remove
sessionStorage.removeItem("session");

๐Ÿ“Š Session Storage vs Local Storage vs Cookies

FeatureSession StorageLocal StorageCookies
Storage Limit~5 MB~5โ€“10 MB~4 KB
LifetimeUntil tab closesUntil manually removedConfigurable
Shared Between TabsโŒ Noโœ… Yesโœ… Yes
Sent to ServerโŒ NoโŒ Noโœ… Yes

โš ๏ธ Limitations

  • ๐Ÿ“ฆ Only strings can be stored directly.
  • ๐Ÿšซ Data is lost when the browser tab or window is closed.
  • ๐ŸŒ Accessible only within the same origin (protocol, domain, and port).
  • ๐Ÿ” Not suitable for storing sensitive information.

Note

Session Storage is isolated per browser tab. Opening the same website in a new tab creates a separate Session Storage instance.

โœ… Best Practices

  • ๐Ÿ“„ Use it for temporary data only.
  • ๐Ÿง  Store objects and arrays using JSON.stringify().
  • ๐Ÿ“ฅ Retrieve complex data using JSON.parse().
  • ๐Ÿงน Remove unused data with removeItem() or clear().
  • ๐Ÿ”’ Never store passwords or confidential information.

๐ŸŽฏ Summary

Session Storage is ideal for storing temporary browser data that should persist only during the current tab session. It offers the same API as Local Storage but automatically clears data when the tab or window is closed, making it perfect for temporary user state, multi-step forms, and session-specific information.