javascript - localStorage and boolean 'string' -
storing boolean value in localstorage, value converted string. trying converting value localstorage boolean, need use json.parse()
method, 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);
i'm quite confused why behaviour. explaination?
ps: using getter/setter localstorage methods doesn't matter here, same result.
local storage stores strings , i'm afraid, whatever input (if feed object, converted automatically standard tostring()
method)... you're doing !! test
on string, true
.
you should use json.stringify()
, json.parse()
when dealing store in dom storage
Comments
Post a Comment