Toolbox-XToolbox-X

Session Storage Utilities

Safely and conveniently interact with the browser's Session Storage API with full type safety and custom serialization.

Overview

A simple set of utilities to interact safely with the browser's Session Storage API. Handles storing, retrieving, and removing typed items scoped to a browser tab session.


getFromSessionStorage

Retrieves a value from session storage, parsing it back into its original type.

Function Signature

function getFromSessionStorage<T>(
  key: string,
  deserialize?: Deserializer<T>
): T | null

Parameters

ParameterTypeDescription
keystringThe key of the item.
deserializeDeserializer<T> (optional)Custom deserialization function. Defaults to JSON.parse.

Returns

  • T | null: The parsed value of type T, or null if the key does not exist or parsing fails.

Example Usage

import { getFromSessionStorage } from 'toolbox-x/dom';

const color = getFromSessionStorage<string>('color');
console.log(color); // → e.g. 'blue'

saveToSessionStorage

Stores a value in session storage, automatically stringifying objects/arrays.

Function Signature

function saveToSessionStorage<T>(
  key: string,
  value: T,
  serialize?: Serializer<T>
): void

Parameters

ParameterTypeDescription
keystringThe key under which to store the value.
valueTThe value to store.
serializeSerializer<T> (optional)Custom serialization function. Defaults to JSON.stringify.

Example Usage

import { saveToSessionStorage } from 'toolbox-x/dom';

saveToSessionStorage('count', 10);
saveToSessionStorage('profile', { id: 7, username: 'user7' });

removeFromSessionStorage

Removes an item from session storage.

Function Signature

function removeFromSessionStorage(key: string): void

Parameters

ParameterTypeDescription
keystringThe key of the item to remove.

Example Usage

import { removeFromSessionStorage } from 'toolbox-x/dom';

removeFromSessionStorage('cart');

  • localStorage — Scopes equivalent utility operations to the browser's persistent storage.

Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)

On this page