JavaScript .removeItem()

anjar.bra's avatar
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.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

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');

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours