I am having trouble understanding some weird behavior in my code and I am wondering if anyone can tell me what I am doing wrong?
I have a Boolean variable isWaiting
and it is saved as false
in localStorage
and when it is retrieved, it is shown as false
but when you do validation, it doesn't see it that way. Here is a small code to replicate my issue:
(function(){
localStorage.setItem("isWaiting", false);
var isWaiting = localStorage.getItem("isWaiting");
console.log("isWaiting = " + isWaiting)
console.log("Is isWaiting False? " + Boolean(isWaiting === false));
// I have tried it as this as well
console.log("Is isWaiting False? " + (isWaiting === false));
})()
Should get:
isWaiting = false
Is isWaiting False? true
Produces:
isWaiting = false
Is isWaiting False? false ⬅ This is clearly wrong, so why is this happening?
What am I doing wrong? Is the boolean value of false somehow getting mangled in the localStorage
process and if so, how would I account for it, if not, then what is wrong?
Solution Update:10/09/18
Based on the kind replies helping me confirm that indeed my suspicion that the LS process is butchering the value type was correct and more specifically the solution provided by @bhojendra-rauniyar, I have changed the code to correct the type right at the moment of retrieval and then handling it as normal throughout the code avoiding having a ton of added code and it seems to have done the trick, thank you to all for your help.
...
var isWaiting = Boolean(JSON.parse(localStorage.getItem("isWaiting")));
...