Storing boolean value in localStorage, this value is converted to string.
Now trying to converting back this value from localStorage to boolean, i need to use JSON.parse()
method, the more handy !!
doesn't work.
Code sample:
var test = false;
localStorage['test'] = test;
console.log("JSON.parse returns: ", JSON.parse(localStorage['test']), "expected: ", test);
console.log("'!!' returns: ", !! localStorage['test'], "expected: ", test);
-jsFiddle-
I'm quite confused why this behaviour. Any explaination?
PS: using getter/setter localStorage methods doesn't matter here, same result.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…