Check if a Key Exists in HTML5 Local Storage



The getitem(key) takes value for one parameter and returns the value associated with the key. The given key is present in the list associated with the object.

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

But if the key is not present in the list then it passes null value by using the below-given code

You can also follow the below-given procedure −

if("user" in localStorage){
   alert('yes');
} else {
   alert('no');
}
Updated on: 2020-06-24T12:30:42+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements