check if localstorage key exists js

JavaScript
if (localStorage.getItem("username") === null) {
  //...
}if("user" in localStorage){
   alert('yes');
} else {
   alert('no');
}
Source

Also in JavaScript: