Sorry if the title is somehow misunderstanding.
I am having some problems with a web app if I try to parse values from a localstorage using json and there are no values, nothing works.
For example:
If you do this: alert(localStorage.getItem("something")); you get null: http://jsfiddle.net/yrGht/
but if you do this it blocks everything: alert(JSON.parse(localStorage["test"])); you get nothing and it stops everything from working: http://jsfiddle.net/7tCaS/
How can I solve this so that I alert the first and if it has no value then keep working because basically I want to set this: JSON.parse(localStorage["test"]) as a global variable but it blocks everything else: http://jsfiddle.net/gq9Ea/
Try this:
The reason it fails is because
JSON.parse(undefined_variable)gives an “invalid character” error. IflocalStorage['test']doesn’t exist, it’sundefined, so it gets an error. The above code putsnullin place if the value is falsy, whichundefinedis. However this does have ill effects if the value is0,""or another falsy value. If more specific code is needed, try: