In my current project I'm using javascripts localStorage in order to store some data. Since this data is parsed afterwards, I need to set it to a default-value if it isn't existing yet. To do so I'm using a simple if-check: Unfortunately, it doesnt work. Here is my code:
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!
How can I solve this problem?
Aucun commentaire:
Enregistrer un commentaire