DEV Community

Deyan Petrov
Deyan Petrov

Posted on

How to check if localStorage item exists

Let's make this short. Since localStorage.getItem("item") returns a null if it is missing, we simply need to check if the return value is not null

if (localStorage.getItem("item") !== null) {
  console.log("Item exists")
  //Do stuff
} else {
  console.log("Item does not exist")
  //Do other stuff
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)