I noticed that I cannot set boolean values in localStorage?
localStorage.setItem("item1", true);
alert(localStorage.getItem("item1") + " | " + (localStorage.getItem("item1") == true));
Always alerts true | false when I try to test localStorage.getItem("item1") == "true" it alerts true … How can I set an item in localStorage to true?
Even if it’s a string, I thought only === would check the type?
So
alert("true" == true); // should be true?
Firefox’s implementation of Storage can only store strings, but on 2009 September, W3C modified the draft to accept any data.
The implementation (still) isn’t caught up yet(see Edit below).So in your case the boolean is converted to a string.
As for why
"true" != true, as written in the description of Equal (==) in MDC*:Note that the string is converted to a Number instead of a Boolean. Since
"true"converted to a number isNaN, it will not be equal to anything, sofalseis returned.(*: For the actual standard, see ECMA-262 §11.9.3 “The Abstract Equality Comparison Algorithm”)
Edit: The
setIteminterface was reverted to accept strings only on the 2011 Sept 1st draft to match the behavior of existing implementations, as none of the vendors are interested in supporting storing non-strings. See https://www.w3.org/Bugs/Public/show_bug.cgi?id=12111 for detail.