10 examples of 'how to get data from localstorage in javascript' in JavaScript

Every line of 'how to get data from localstorage in javascript' 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
256function getLocalData(key) {
257 if (window.localStorage) {
258 return window.localStorage.getItem(key);
259 }
260}
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

38function getLocalStorage(key) {
39 return localStorage.getItem(key);
40};
311function getLocalStorage (currentControl, key) {
312 if (key in store) {
313 const stored = store[key];
314 if (!stored) return undefined;
315 if (currentControl.type !== stored.type) return undefined;
316 if (currentControl.value !== stored.defaultValue) return undefined;
317 // console.log('CHECK CHECK', key, currentControl.value, stored.defaultValue)
318 return stored.value;
319 }
320 return undefined;
321}
39function getValueFromLocalStorage() {
40 if (typeof localStorage === "undefined") {
41 return null;
42 }
43 const storedValue = localStorage.getItem(key) || "null";
44 try {
45 return JSON.parse(storedValue);
46 } catch (err) {
47 console.error(err);
48 }
49 return storedValue;
50}
53function GetFromLocalStorage(): LocalStorageAppContext | null {
54 const localStorageAppContextJson = localStorage.getItem(appContextKey);
55
56 if (localStorageAppContextJson) {
57 const localStorageAppContext = JSON.parse(localStorageAppContextJson);
58
59 return localStorageAppContext;
60 }
61
62 return null;
63}
134export function getStorageData(key) {
135 var item = null;
136 try {
137 if (window.localStorage) {
138 item = window.localStorage.getItem(key);
139 }
140 } catch (e) {
141 }
142 return item;
143}
163function getLocalStorageStr(key, defaultValue) {
164 return window.localStorage[key] || defaultValue;
165}
117function getLocalStorageItem(key) {
118 try {
119 return JSON.parse(
120 window.localStorage.getItem(key)
121 );
122 } catch (e) {
123 return null;
124 }
125}
11function getLocalStorageObject() {
12 const localStorageObject = LocalStorageUtil.get(LOCAL_STORAGE_KEY);
13 if (localStorageObject != null) {
14 try {
15 return JSON.parse(localStorageObject);
16 } catch (e) {
17 return null;
18 }
19 }
20 return null;
21}
9export function loadFromLocalStorage (key) {
10 return window.localStorage.getItem(key)
11}

Related snippets