sessionStorage.setItem
Adds a key/value pair to the sessionStorage object for the duration of the page session
Syntax
sessionStorage.setItem(key, value)Parameters
| Parameter | Type | Description |
|---|---|---|
| key | string | The name of the key |
| value | string | The value to store |
Return Value
undefined
Examples
sessionStorage.setItem('currentStep', '3')
console.log(sessionStorage.getItem('currentStep')) // '3'function saveFormDraft(formId: string, data: Record<string, string>) {
sessionStorage.setItem(`draft_${formId}`, JSON.stringify(data))
}sessionStorage.setItem('sessionStart', new Date().toISOString())
console.log('Session started at:', sessionStorage.getItem('sessionStart'))Understanding sessionStorage.setItem
The sessionStorage.setItem method in JavaScript adds a key/value pair to the sessionStorage object for the duration of the page session. It belongs to the Storage object and is one of the most widely used methods for working with storage values in modern JavaScript and TypeScript applications.
The method signature is sessionStorage.setItem(key, value). It accepts 2 parameters: key, value. When called, it returns undefined. Understanding when and how to use setItem() helps you write more expressive, readable code.
Common use cases for sessionStorage.setItem include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like sessionstorage-getitem, sessionstorage-removeitem, localstorage-setitem, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for sessionStorage.setItem is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
sessionStorage.getItemReturns the value associated with the given key in the sessionStorage, or null if the key does not exist
sessionStorage.removeItemRemoves the key/value pair for the given key from the sessionStorage object
localStorage.setItemAdds a key/value pair to the localStorage object, or updates the value if the key already exists
More Storage Methods
Other methods in the Storage object
Related Tools
More Storage Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.