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

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.