634 B
634 B
title, tags
| title | tags |
|---|---|
| isLocalStorageEnabled | browser,intermediate |
Checks if localStorage is enabled.
- Use a
try...catchblock to returntrueif all operations complete successfully,falseotherwise. - Use
Storage.setItem()andStorage.removeItem()to test storing and deleting a value inwindow.localStorage.
const isLocalStorageEnabled = () => {
try {
const key = `__storage__test`;
window.localStorage.setItem(key, null);
window.localStorage.removeItem(key);
return true;
} catch (e) {
return false;
}
};
isLocalStorageEnabled(); // true, if localStorage is accessible