Differentiate between local and global variables in JavaScri
Differentiate between local and global variables in JavaScript and give a suitable example.
Solution
Local Variables
Global Variables
Variables declared within a JavaScript function are called local variables.
Variables declared outside a JavaScript function are called global variables.
Local variable have local scope, which means that these variables can be accessed only within the function in which the variable is declared.
Global variable have global scope, which means that these variable can be accessed in any function in the webpage can access it.
Example:
function myExample()
{
var greeting=”Hello!”;
}
Here greeting is a local variable and can be accessed only in myExample.
Example:
var greeting=”Hello!”;
function myExample()
{
document.write(greeting);
}
Function myExample2()
{
document.write(greeting);
}
Here greeting is a global variable and can be used in any function in the webpage.
Since local variables have local scope, any other function can declare a variable with the same name.
Example:
function myExample()
{
var greeting=”Hello!”;
}
function myExample2()
{
var greeting=”Hi”;
}
Here greeting is a variable which is local to myExample(), as it is a local variable we can declare greeting in myExample2() also. ‘greeting’ variable has a different meaning in both the functions
Since global variables have global scope we cannot declare a variable in a function whose name is same as the global variable.
Example:
var greeting=”Hello!”;
function myExample()
{
var greeting=”Hi”; //this is not allowed
}
Here greeting is a global variable so declaring a variable with the same greeting in anyother function is illegal.
Local variables are created when they are declared and are deleted when the function ends
Global variables are created when they are declared and are deleted when the webpage ends
| Local Variables | Global Variables |
| Variables declared within a JavaScript function are called local variables. | Variables declared outside a JavaScript function are called global variables. |
| Local variable have local scope, which means that these variables can be accessed only within the function in which the variable is declared. | Global variable have global scope, which means that these variable can be accessed in any function in the webpage can access it. |
| Example: function myExample() { var greeting=”Hello!”; } Here greeting is a local variable and can be accessed only in myExample. | Example: var greeting=”Hello!”; function myExample() { document.write(greeting); } Function myExample2() { document.write(greeting); } Here greeting is a global variable and can be used in any function in the webpage. |
| Since local variables have local scope, any other function can declare a variable with the same name. Example: function myExample() { var greeting=”Hello!”; } function myExample2() { var greeting=”Hi”; } Here greeting is a variable which is local to myExample(), as it is a local variable we can declare greeting in myExample2() also. ‘greeting’ variable has a different meaning in both the functions | Since global variables have global scope we cannot declare a variable in a function whose name is same as the global variable. Example: var greeting=”Hello!”; function myExample() { var greeting=”Hi”; //this is not allowed } Here greeting is a global variable so declaring a variable with the same greeting in anyother function is illegal. |
| Local variables are created when they are declared and are deleted when the function ends | Global variables are created when they are declared and are deleted when the webpage ends |


