Favorites

DOM Storage.setItem() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Storage.setItem()
DOM Storage MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The setItem() method saves a value under a key in localStorage or sessionStorage, replacing any existing value.

Description

The setItem() method stores a value under a key, adding it or updating the existing one. Values are always converted to strings, so objects become "[object Object]" unless you use JSON.stringify(). It throws a QuotaExceededError when storage is full, typically at around 5 MB.

Writing the same value again still fires a storage event in other tabs, so avoid unnecessary writes if other tabs are listening.

Storage access can throw, for example in some private browsing modes or in sandboxed iframes, so wrap calls in try...catch where it matters. Both localStorage and sessionStorage support the method, and changes made in one tab fire a storage event in other tabs on the same origin.

Syntax

storage.setItem(keyName, keyValue)

Parameters

  • keyNameThe key to store the value under.
  • keyValueThe value to store. It is converted to a string.

Return Value

None (undefined).

Exceptions

  • QuotaExceededErrorThrown if the storage is full.

Example

try {
  localStorage.setItem('user', JSON.stringify({ name: 'Sam', theme: 'dark' }));
  localStorage.setItem('count', 5);
  console.log(typeof localStorage.getItem('count'));
  console.log(JSON.parse(localStorage.getItem('user')).theme);
} catch (e) {
  console.log('Storage unavailable:', e.name);
}

Browser Support

The following information will show you the current browser support for the Storage.setItem() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee