How to check if a key exists in local storage using JavaScript

published: 16 Aug 2022

2 min read

Check if a key exists in local storage using JavaScript

To check if a key exists in HTML local storage by using JavaScript, you can use the getItem() method of the localStorage object.

The getItem() method returns the current value associated with the given key. If the key does not exist, getItem() returns null which is sufficient to verify if the key exists or not:

if (localStorage.getItem('email') !== null) {
    console.log('Email address exists');
} else {
    console.log('Email address not found');
}

Take a look at this article to learn more about HTML web storage API and how to use localStorage and sessionStorage objects to store data in the user's browser.

How to check if a key exists in local storage using JavaScript | Coding Tips And Tricks

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: javascript, programming
Check out what's on in the tag: javascript, programming