Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable.
You can override this behaviour by declaring it locally using var, but if you don't use var, then a variable name used in a function will be global if that variable has been declared globally.
That's why it's considered best practice to always declare your variables explicitly with var. Because if you forget it, you can start messing with globals by accident. It's an easy mistake to make. But in your case, this turn around and becomes an easy answer to your question.
Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable.
You can override this behaviour by declaring it locally using var, but if you don't use var, then a variable name used in a function will be global if that variable has been declared globally.
That's why it's considered best practice to always declare your variables explicitly with var. Because if you forget it, you can start messing with globals by accident. It's an easy mistake to make. But in your case, this turn around and becomes an easy answer to your question.
var a = 10;
myFunction();
function myFunction(){
a = 20;
}
alert("Value of 'a' outside the function " + a); //outputs 20
javascript - jquery change global variable inside function - Stack Overflow
How do I update a global variable from inside of an arrow function?
Not Clear How a Function Can Overwrite a Global Variable
update global variable from a function in the javascript - Stack Overflow
Videos
Yes, it is possible, but remember to NOT put the var keyword in front of it inside the function.
ERORR - DOES NOT WORK:
var variable = "before";
change();
alert(variable);
function change() {
var variable = "after";
}
WORKS:
var variable = "before";
change();
alert(variable);
function change() {
variable = "after";
}
You should avoid declaring global variables since they add themselves as properties to the window. However, to answer your question, yes you can change global variables by setting either changing variable or window.variable.
Example: http://jsbin.com/xujenimiwe/3/edit?js,console,output
var variable = "before"; // will add property to window -- window.variable
console.log(variable);
change();
console.log(window.variable);
function change(){
variable = "after"; // can also use window.variable = "after"
}
Please let me know if you have any questions!
I am trying to write a module which will get the location from the browser (navigator.geolocation) and returns the result.
But the "location" I get like this only lives inside the function it calls. How do I return the location?
function getLocation() {
let loc = null;
if (navigator) {
navigator.geolocation.getCurrentPosition(
({ coords }) => {
loc = {
"latitude": coords.latitude,
"longitude": coords.longitude
};
console.log("loc inside function as desired:", loc);
}
);
}
console.log("but loc is 'null' here:", loc);
return loc;
}
export default getLocation;