我如何检查一个项目是否设置在localStorage?目前我正在使用

if (!(localStorage.getItem("infiniteScrollEnabled") == true || localStorage.getItem("infiniteScrollEnabled") == false)) {
    // init variable/set default variable for item
    localStorage.setItem("infiniteScrollEnabled", true);
}

当前回答

试试这段代码

if (localStorage.getItem("infiniteScrollEnabled") === null) {

} else {

}

其他回答

if(!localStorage.hash) localStorage.hash = "thinkdj";

Or

var secret =  localStorage.hash || 42;

为真正的

localStorage.infiniteScrollEnabled = 1;

因为假

localStorage.removeItem("infiniteScrollEnabled")

检查存在

if (localStorage[""infiniteScrollEnabled""]) {
  //CODE IF ENABLED
}

试试这段代码

if (localStorage.getItem("infiniteScrollEnabled") === null) {

} else {

}
localStorage['root2']=null;

localStorage.getItem("root2") === null //false

也许最好先看一遍计划?

localStorage['root1']=187;
187
'root1' in localStorage
true

我总是通过检查localStorage或sessionStorage的长度来检查它们是否被设置

if (sessionStorage.length > 0) {
  console.log("exists")
} else {
  console.log("not exists")
}