'Global variable & Functions [closed]
In Javascript...If a variable is defined globally and I use a function to manipulate the value of that variable - do I have to return at the end of the function or does it matter since the variable is global?
Solution 1:[1]
I just experimented with the situation you describe and it appears that you do not need to return the global variable from the function in order for the variable to be altered by the function. Please see the code and run it.
var myVar = 'x';
function myFunction(){
myVar = 'y';
}
myFunction();
console.log(myVar);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user3425506 |
