When working with web storage, it's crucial to verify the existence of specific items before accessing or modifying them. In this case, we want to determine whether a particular item is set in localStorage.
The current method for checking the existence of an item appears to be:
if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
// init variable/set default variable for item
localStorage.setItem("infiniteScrollEnabled", true);
}
However, a simplified and more efficient way to check for the existence of an item is to utilize the null return value of the getItem method. According to the WebStorage specification, if the item doesn't exist in the storage, getItem explicitly returns null.
Therefore, you can use the following code to check for the existence of an item:
if (localStorage.getItem("infiniteScrollEnabled") === null) {
//...
}
For further information on this topic, you may find the following resource helpful:
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3