Published Aug 2, 2023
Contribute to Docs

.removeItem() is a method provided by the Web Storage API in JavaScript. It allows you to remove a specific key-value pair from the local storage or session storage based on the specified key.



When called, the .removeItem() method takes a single parameter, keyName, which is the key of the item to be removed. The function removes the associated key-value pair from storage, if found. If the key does not exist, the method does nothing and the storage remains unchanged.


The following code demonstrates the application of .removeItem() to modify localStorage:

localStorage.setItem('username', 'JohnDoe');

In the example above, a key-value pair (username, JohnDoe) is added to the localStorage using .setItem(), and removed using .removeItem().

The same can be done with the session storage:

sessionStorage.setItem('username', 'JohnDoe');

All contributors

Looking to contribute?

Learn JavaScript on Codecademy