Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local and global variables inside a JavaScript function

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?

like image 886
Iggy Avatar asked Oct 12 '16 18:10

Iggy


People also ask

What is the difference between local and global variables in JavaScript?

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.

How to declare a local variable in JavaScript?

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.

Can You overwrite a global variable in JavaScript?

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.

How to use variables within a JavaScript function?

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.


2 Answers

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.

like image 133
Kevin L Avatar answered Sep 27 '22 20:09

Kevin L


This is to do with variable hoisting

The code in your second example is executed in this order:

  1. Declare global variable text
  2. Set value of global variable text to "top"
  3. Declare function print
  4. Call function print
  5. Declare local variable text (due to hoisting)
  6. Return value of local variable text (undefined at this point)
  7. Set value of local variable 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.

like image 28
James Monger Avatar answered Sep 27 '22 18:09

James Monger