"Load" function not setting variable

user12876806

I have two functions that save variables to LocalStorage. The first one saves a variable, "money", but the second doesn't. The global variable "money" does not get assigned.

function saveVars() {
    localStorage.setItem('money', money);
}

function loadVars() {
    var money = localStorage.getItem('money');
}
Touffy

From your example, I can assume two alternatives :

1) you want to "load" the value into a global variable named "money" ? Your code doesn't work because you declare a local variable "money" that only exists within the function loadVars. Don't declare your variable (i.e. do not add the keyword var or any equivalent in the function). The assignment money = … will assign to the global variable "money" only if there is no other variable named "money" in a more specific scope — and it will do so even if the global variable was not declared.

function loadVars() {
    money = localStorage.getItem('money');
}

2) you want loadVars to return the value of the stored variable ? and then you can assign that to a global:

function loadVars() {
    return localStorage.getItem('money');
}

var money = loadVars();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Golang Setting variable in global var with structs in function

Setting variable through protected function - lost reference

Setting a value of class variable from another function

JavaScript setting value to variable properties on a function call

js Setting function as variable or anonymous function returning the function

Powershell - Setting a variable inside a function not setting it globally?

Setting Connection String as App Setting/Environment Variable in Azure Function

In ColdFusion, what is the difference between setting a function to a variable and calling a function in hashtags?

Setting a variable equal to a function without parenthesis?

Setting variable to a return from a callback function

Get variable inside load function

Setting a class variable from a global function

Bash - Setting function value as a variable

Setting up variable to return function value or false

Setting a global scope variable inside a function

Setting a value in a function as a variable outside the function (Swift)

Swift setting variable inline or in function?

Get variable value of a function without setting it globally

Setting a variable's value directly from a function

How setting a variable in img.onload function

Setting component scope variable in a nested function

Setting function parameter as global variable

Setting a variable equal to a return of function

Python function setting variable unexpectedly

Setting column contents as variable in function (R)

Error in setting a value to useState variable in function

Setting status code for exception raised in load() function in SvelteKit

Setting a variable in function outside of the function in python

Setting variable to output of awk function