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.


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 the key.


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:

Gender : male

All contributors

Looking to contribute?

Learn JavaScript on Codecademy