localStorage has many gotchas around it like different behavior in different browsers when localStorage is full also in browsers like safari you can't access or set localStorage when in incognito mode.
#ActuallyAutistic web dev. Does front of the front-end. Loves perf and minimalism. Prefers HTML, CSS, Web Standards over JS, UX over DX. Hates div disease.
And here is the shortest code to check to see if you can actually use the localStorage:
// usage: put return value to a variable or use as a if condition!function(l){try{(l=localStorage).removeItem(l._='_')}catch(e){return1}}()
You could also write a wrapper that would take care of cases like running out of localStorage space, but that is quite an overkill for most use cases. In the other hand if you do have lots of localStorage usage and/or need the abstraction (for code readability/maintenance reasons) then it will probably be worth it over a simple boolean check like the above.
localStorage has many gotchas around it like different behavior in different browsers when localStorage is full also in browsers like safari you can't access or set localStorage when in incognito mode.
It all comes with downsides indeed!
Not the most reliable storage, but a good alternative to keep in mind.
And here is the shortest code to check to see if you can actually use the localStorage:
You could also write a wrapper that would take care of cases like running out of localStorage space, but that is quite an overkill for most use cases. In the other hand if you do have lots of localStorage usage and/or need the abstraction (for code readability/maintenance reasons) then it will probably be worth it over a simple boolean check like the above.
Very nice quick check! Love it <3