I recently asked a question about LocalStorage. Using JSON.parse(localStorage.item)
and JSON.parse(localStorage['item'])
weren't working to return NULL
when the item hadn't been set yet.
However, JSON.parse(localStorage.getItem('item')
did work. And it turns out, JSON.parse(localStorage.testObject || null)
also works.
One of the comments basically said that localStorage.getItem()
and localStorage.setItem()
should always be preferred:
The getter and setter provide a consistent, standardised and
crossbrowser compatible way to work with the LS api and should always
be preferred over the other ways. -Christoph
I've come to like using the shorthand dot and bracket notations for localStorage, but I'm curious to know others' take on this. Is localStorage.getItem('item') better than localStorage.item or localStorage['item'] OR as long as they work are the shorthand notations okay?