【发布时间】:2015-03-06 20:27:00
【问题描述】:
在我当前的项目中,我使用 javascripts localStorage 来存储一些数据。由于之后会解析此数据,因此如果它尚不存在,我需要将其设置为默认值。为此,我使用了一个简单的 if-check:不幸的是,它不起作用。这是我的代码:
localStorage.setItem("myItem", null); //Test for the if-check. But even without it isnt working.
if(localStorage.getItem("myItem") == undefined || localStorage.getItem("myItem") == null || localStorage.getItem("myItem") == ""){
console.log("is null");
localStorage.setItem("myItem", "myDefaultContent");
}
console.log(localStorage.getItem("myItem")); //null!
我该如何解决这个问题?
【问题讨论】:
-
运行
typeof localStorage.getItem("myItem")
标签: javascript if-statement null local-storage