I am having a LocalStorage item which I am parsing into a variable like this:
let resp = JSON.parse(localStorage.getItem('someKey'));
The thing is sometimes, the localStorage may not have the value and it may return an Undefined object to parse which is resulting in following error:
SyntaxError: Unexpected token U in JSON at position 0
I have tried this:
let resp = localStorage.getItem('someKey') ? JSON.parse(localStorage.getItem('someKey')) : null;
But I believe this is not very optimal way of handling this, any suggestions are welcome