In the Chrome console I set foo
to null:
localStorage.setItem("foo",null)
Then I test, whether it is null:
console.log(localStorage.getItem("foo")==null)
prints false
.
Then I test, whether it is the string "null"
:
console.log(localStorage.getItem("foo")=="null")
prints true
.
I thought that null
was a legitimate Javascript value. Storing it as the string "null"
is very counter intuitive and caused a strange bug in an otherwise working program, when I cleared the localStorage in the browser manually.
Please see https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
All values are stored as strings in local storage. You should stringify data before storing it and parse data after retrieving it:
As per spec,
localstorage
uses Storage object interfacesetter method translates to
setItem
, accepts onlyDOMString
As per documentation