DOM Storage.setItem() Method
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
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.
Desktop





Tablets & Mobile





