I have here a simple function and a global variable.
Why is
myname
undefined
and not the string"global"
?
var myname = "global"; // global variable
function func() {
alert(myname); // "undefined"
var myname = "local";
alert(myname); // "local"
}
func();
Is not possible to refer to a outer variable that is define outside the scope of that function? And in this a global variable...
And how I can fix this so I don't get a undefined
from a global variable?
The reason the first alert is undefined is because you re-declared global as a local variable below it in the function. And in javascript that means from the top of the function it is considered the local variable.
Using global variables causes very tight coupling of code. Using global variables causes namespace pollution. This may lead to unnecessarily reassigning a global value. Testing in programs using global variables can be a huge pain as it is difficult to decouple them when testing.
It is usually not a good programming practice to give different variables the same names. If a global and a local variable with the same name are in scope, which means accessible, at the same time, your code can access only the local variable.
Global variables, as the name implies, are variables that are accessible globally, or everywhere throughout the program. Once declared, they remain in memory throughout the runtime of the program. This means that they can be changed by any function at any point and may affect the program as a whole.
You have just stumbled on a js "feature" called hoisting
var myname = "global"; // global variable
function func() {
alert(myname); // "undefined"
var myname = "local";
alert(myname); // "local"
}
func();
In this code when you define func
the compiler looks at the function body. It sees that you are declaring a variable called myname
.
Javascript Hoists variable and function declarations, by moving the declaration to the top of the function.
Because of hoisting your code is rewritten to the following.
var myname = "global"; // global variable
function func() {
var myname; //declare local variable and assign it undefined
alert(myname); // "undefined"
myname = "local"; // assign local var myname to "local"
alert(myname); // "local"
}
func();
This "Covers" the global variable. If you want access to the global variable within the scope of a function use the this
keyword.
var myname = "global"; // global variable
function func() {
var myname = "local";
alert(this.myname); // "global"
alert(myname); // "local"
}
func();
Note that this only works in calling a function not a method or constructor because the this
keyword changes what its bound to based on how you call a function.
EDIT: For completeness
If you want to get access to global variables in any context regardless of function type then declare a global variable that by convention you never cover.
var global = this; // in global scope.
var myname = "global";
var obj = {f: function () {
var myname = "local";
console.log(global.myname);
}};
obj.f(); // "global"
Note that this is in method position and the this
keyword refers to obj directly and therefore doesn't have myname defined.
Inside a function, you're declaring var myname = "local"
. Even though you're doing it in the middle of the method, that variable has function scope, so it belongs to the entire function, even the code above it.
So the local variable's value is undefined before that line, and has a value after, but neither one are touching the global variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With