.setItem()
Published Aug 2, 2023
Contribute to Docs
The .setItem()
method updates the value to the specified storage object key, if it exists, else adds the key to the specified storage object and returns undefined
.
Syntax
localStorage.setItem(key, value);
sessionStorage.setItem(key, value);
Both key
and value
are required parameters.
- The
key
is the name of the key to create. - The
value
is the value to create/update thekey
.
Example
This example demonstrates a basic implementation of the .setItem()
method:
console.log(localStorage.setItem('name', 'mark'));localStorage.setItem('gender', 'male');console.log('Gender : ', localStorage.gender);
The output will be:
undefinedGender : male
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.