removeItem() Method – How to Remove Item from Web Storage
The removeItem() method removes a property from the specified web storage.
Syntax of the removeItem()
Method
Section titled “Syntax of the removeItem() Method”removeItem()
accepts one required argument. Here is the syntax:
webStorageObject.removeItem(key);
webStorageObject
represents the storage object whose item you wish to remove—that is,localStorage
orsessionStorage
.key
is the only argument accepted byremoveItem()
. It is a required string argument specifying the name of the web storage property you want to remove.
Examples
Section titled “Examples”Below are examples of the removeItem()
method.
How to remove data from session storage
Section titled “How to remove data from session storage”- Invoke
sessionStorage
’sremoveItem()
method. - Provide the name of the data you wish to remove.
// Store carColor: "Pink" in the session storage:sessionStorage.setItem("carColor", "Pink");
// Store pcColor: "Yellow" in the session storage:sessionStorage.setItem("pcColor", "Yellow");
// Store laptopColor: "White" in the session storage:sessionStorage.setItem("laptopColor", "White");
// Remove the pcColor item from the session storage:sessionStorage.removeItem("pcColor");
// Confirm whether the pcColor item still exists in the session storage:sessionStorage.getItem("pcColor");
// The invocation above will return: null
How to remove data from the local storage object
Section titled “How to remove data from the local storage object”- Invoke
localStorage
’sremoveItem()
method. - Provide the name of the data you wish to remove.
// Store carColor: "Pink" in the local storage:localStorage.setItem("carColor", "Pink");
// Store pcColor: "Yellow" in the local storage:localStorage.setItem("pcColor", "Yellow");
// Store laptopColor: "White" in the local storage:localStorage.setItem("laptopColor", "White");
// Remove the pcColor item from the local storage:localStorage.removeItem("pcColor");
// Confirm whether the pcColor item still exists in the local storage:localStorage.getItem("pcColor");
// The invocation above will return: null