javascript change global variable in function code example
Example 1: javascript define global variable
window.myGlobalVariable = "I am totally a global Var";
var myOtherGlobalVariable="I too am global as long as I'm outside a function";
Example 2: create global variable inside function JavaScript
<script>
function foo() {
window.yourGlobalVariable = ...;
}
</script>
Example 3: change a variable outside a function js
var global = "Global Variable";
function setGlobal(){
global = "Hello World!";
};
setGlobal();
console.log(global);