Categories
JavaScript Answers

How to check if a key exists with HTML5 LocalStorage and JavaScript?

Spread the love

Sometimes, we want to check if a key exists with HTML5 LocalStorage and JavaScript.

In this article, we’ll look at how to check if a key exists with HTML5 LocalStorage and JavaScript.

How to check if a key exists with HTML5 LocalStorage and JavaScript?

To check if a key exists with HTML5 LocalStorage and JavaScript, we can use the getItem method.

For instance, we write

if (localStorage.getItem("username") === null) {
  //...
}

to check if the entry with key 'username' exists or not with getItem.

If it returns null, then the entry doesn’t exist.

Conclusion

To check if a key exists with HTML5 LocalStorage and JavaScript, we can use the getItem method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *