Files
30-seconds-of-code/snippets/isLocalStorageEnabled.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

732 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Check if localStorage is enabled browser intermediate 2020-12-31T13:13:47+02:00 2020-12-31T13:13:47+02:00

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.
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