.removeItem()
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.
Syntax
localStorage.removeItem('keyName');
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.
Example
The following code demonstrates the application of .removeItem()
to modify localStorage
:
localStorage.setItem('username', 'JohnDoe');localStorage.removeItem('username');
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');sessionStorage.removeItem('username');
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.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours