Sometimes, we want to check whether a local storage item is set with JavaScript.
In this article, we’ll look at how to check whether a local storage item is set with JavaScript.
How to check whether a local storage item is set with JavaScript?
To check whether a local storage item is set with JavaScript, we use the getItem
method.
For instance, we write
if (localStorage.getItem("infiniteScrollEnabled") === null) {
//...
}
to call localStorage.getItem
with the key of the item we want to get.
If it returns null
, then the entry with the key doesn’t exist.
Conclusion
To check whether a local storage item is set with JavaScript, we use the getItem
method.