10 examples of 'check if localstorage exists' in JavaScript

Every line of 'check if localstorage exists' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
54export function localStorageExists() {
55 return lsExists;
56}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

257function checkForLocalStorage() {
258 try {
259 localStorage.setItem('test', 'test');
260 localStorage.removeItem('test');
261 return true;
262 } catch (e) {
263 return false;
264 }
265}
35function isLocalStorageAvailable() {
36 try {
37 return 'localStorage' in window && window['localStorage'] !== null;
38 } catch (e) {
39 console.log("localStorage_failed:", e);
40 return false;
41 }
42};
331checkIsLocalStorageSupported_() {
332 try {
333 if (!('localStorage' in this.win)) {
334 return false;
335 }
336
337 // We do not care about the value fetched from local storage; we only care
338 // whether the call throws an exception or not. As such, we can look up
339 // any arbitrary key.
340 this.win.localStorage.getItem('test');
341 return true;
342 } catch (e) {
343 return false;
344 }
345}
18function exists (filename, callback) {
19 localforage.getItem(filename, function (err, value) {
20 if (value !== null) { // Even if value is undefined, localforage returns null
21 return callback(true);
22 } else {
23 return callback(false);
24 }
25 });
26}
115export function localStorageAvailable() {
116 try {
117 const storage = window.localStorage;
118 const x = '__storage_test__';
119 storage.setItem(x, x);
120 storage.removeItem(x);
121 return true;
122 } catch (e) {
123 return false;
124 }
125}
44exists(key) {
45 return this.get(this.getKey(key)) || false;
46}
44exists() {
45 return fse.pathExists(this.registryFile);
46}
38has(key: string) {
39 try {
40 return localStorage.getItem(key) != null;
41 } catch (err) {
42 console.error(err);
43 return false;
44 }
45}
84public static isAvailable(): boolean {
85 return typeof global.localStorage !== 'undefined';
86}

Related snippets