LocalStorage Get Item With Dot/Bracket Notation Not Working With JSON.parse()
If possible, I would like to be able to use shorthand notation to get an item from localStorage and then use JSON.parse() on it. In my code below, if I use the following it works:
Solution 1:
The problem occurs the first time, when the localStorage.testObject
is not yet defined..
In that case localStorage.testObject
is undefined and JSON.parse
fails with that argument
On the other hand the getItem
method handles this internally and returns null
..
You could use JSON.parse(localStorage.testObject || null)
Post a Comment for "LocalStorage Get Item With Dot/Bracket Notation Not Working With JSON.parse()"