Files
30-seconds-of-code/snippets/isLocalStorageEnabled.md
2020-12-31 13:13:47 +02:00

27 lines
634 B
Markdown

---
title: isLocalStorageEnabled
tags: browser,intermediate
---
Checks if `localStorage` is enabled.
- Use a `try...catch` block to return `true` if all operations complete successfully, `false` otherwise.
- Use `Storage.setItem()` and `Storage.removeItem()` to test storing and deleting a value in `window.localStorage`.
```js
const isLocalStorageEnabled = () => {
try {
const key = `__storage__test`;
window.localStorage.setItem(key, null);
window.localStorage.removeItem(key);
return true;
} catch (e) {
return false;
}
};
```
```js
isLocalStorageEnabled(); // true, if localStorage is accessible
```