I am learning JavaScript global and local variables, but I am confused on this particular function.
var text = "top";
function print() {
return (text);
}
print();
// Returns 'top'
I understands why it returns top. var text
is a global variable. print()
function has access to it and returns text
, thus returning 'top'
.
var text = "top";
function print() {
return (text);
var text = "bottom";
}
print();
// Returns undefined
I have a basic knowledge of global and local variables (or so I thought). I know that the function print
has access to its own local plus global variables.
I don't understand why this returns undefined
. To my understanding, the line return text;
retrieves global variable text
, which it has access to (as shown on the first code block). After returning text = 'top'
, it also declares its own local variable with the same name but different value, 'bottom'
. The local variable bottom
, to my knowledge, ought to sit there because it wasn't called earlier.
Why didn't it show top
(or even shows bottom
), but instead shows undefined
?
Local Variable: When you use JavaScript, local variables are variables that are defined within functions. They have local scope, which means that they can only be used within the functions that define them. Global Variable: In contrast, global variables are variables that are defined outside of functions.
A local variable in JavaScript is declared inside a block or a function. These variables are accessible within the function or block only. Furthermore, local variables are bound to their value within the local scope only. JavaScript has two types of variables, i.e. Global and Local Variables.
Your global variables (or functions) can overwrite window variables (or functions). Any function, including the window object, can overwrite your global variables and functions. The lifetime of a JavaScript variable starts when it is declared. Function (local) variables are deleted when the function is completed.
Variables declared within a JavaScript function, become LOCAL to the function. They can only be accessed from within the function. Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.
JavaScript hoists your variable declaration such that your code is functionally the following:
var text = "top";
function print() {
var text;
return (text);
// Unreachable code below
text = "bottom";
}
print();
// Returns undefined
Since text
, as declared in your function, is not yet defined when you hit return(text)
, and text="bottom"
is unreachable, print()
returns undefined
.
See What is the scope of variables in JavaScript? for more. This question relates to case 7.
This is to do with variable hoisting
The code in your second example is executed in this order:
text
text
to "top"print
print
text
(due to hoisting)text
(undefined
at this point)text
to "bottom"It is executed as if it were written like this:
var text = "top";
function print() {
var text;
return (text);
text = "bottom";
}
print();
As you can see, the value of text
is returned before actually being defined, and therefore it is undefined
.
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