So I'm playing around with JavaScript and came across what I think to be an oddity. Is anyone able to explain the following? (i've included the alerted values as comments)
Why is the first alert(msg) inside foo() returning undefined and not outside?
var msg = 'outside';
function foo() {
alert(msg); // undefined
var msg = 'inside';
alert(msg); // inside
}
foo();
alert(msg); // outside
Considering these both work fine:
var msg = 'outside';
function foo() {
alert(msg); // outside
}
alert(msg); // outside
and:
var msg = 'outside';
function foo() {
var msg = 'inside';
alert(msg); // inside
}
alert(msg); // outside
This is because global variables are easily overwritten by other scripts. Global Variables are not bad and not even a security concern, but it shouldn't overwrite values of another variable. On the usage of more global variables in our code, it may lead to a maintenance issue.
Automatically GlobalIf you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable. This code example will declare a global variable carName , even if the value is assigned inside a function.
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.
In Javascript, it doesn't matter how many times you use the keyword “var”. If it's the same name in the same function, you are pointing to the same variable. This function scope can be a source of a lot of bugs.
What is happening in your first example is the declaration and initlization of msg are being split with the declaration being hoisted to the top of the closure.
var msg; //declaration
msg = "inside" //initialization
Therefore the code you wrote is the same thing as
var msg = 'outside';
function foo() {
var msg;
alert(msg); // undefined
msg = 'inside';
alert(msg); // inside
}
The second example is not the same. In your second example you have not declared a local variable msg so alert(msg) refers to the global msg. Here is some further reading on: Hoisting
Variable declarations inside a Javascript closure take place first, regardless of where they are located within the the closure. So in your first example, a local variables msg
exists at the start of the function (because it is declared within the function) but a value is not assigned to it until after the first alert
, so for that first alert, msg
is undefined.
Your first example is equivalent to:
var msg = 'outside';
function foo() {
var msg;
alert(msg); // undefined
msg = 'inside';
alert(msg); // inside
}
alert(msg); // outside
In the second example, you don't explicitly declare msg
within the function. Since there is already a global variable with the same name, the global is used instead of a local being defined.
In the third example, you explicitly declare the variable and assign it a value before trying to use it, so when you alert(msg)
, the local, defined variable is alerted.
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