Storage setItem() method
- Previous Page getItem()
- Next Page removeItem()
- Go Back to the Previous Level Storage Object
Example
Set the value of a specified local storage item:
localStorage.setItem("mytime", Date.now());
Definition and Usage
setItem() method sets the value of the specified storage object item.
setItem() method belongs to the Storage object, which can be localStorage object, which can also be sessionStorage object.
Browser Support
Method | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
setItem() | 4 | 8 | 3.5 | 4 | 10.5 |
Syntax
localStorage.setItem(keyname, value)
or:
sessionStorage.setItem(keyname, value)
Parameter Values
Parameters | Description |
---|---|
keyname | Required. String, specifying the name of the key to be set. |
value | Required. String, specifying the value of the key to be set. |
Technical Details
DOM Version: | Web Storage API |
---|---|
Return value: | String value, representing the inserted value. |
More examples
Example
The same example, but using session storage instead of local storage.
Set the value of a specified session storage item:
sessionStorage.setItem("test1", "Lorem ipsum");
Example
You can also use dot notation (obj.key) to set the value:
sessionStorage.test1 = "Lorem ipsum";
Example
You can also set the value like this:
sessionStorage["test1"] = "Lorem ipsum";
Related Pages
Web Storage Reference Manual:getItem() Method
Web Storage Reference Manual:removeItem() Method
- Previous Page getItem()
- Next Page removeItem()
- Go Back to the Previous Level Storage Object