How to check if localStorage item exists

webdeecoder

Deyan Petrov

Posted on August 7, 2022

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
💖 💪 🙅 🚩
webdeecoder
Deyan Petrov

Posted on August 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related