Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

JavaScript Scope

Scope in JavaScript defines accessibility of variables, objects and functions.

There are two types of scope in JavaScript
1. Global Scope
2. Local Scope


Global Scope

Variables declared outside of any function become global variables. Global variables can be accessed and modified from any function.

Example: Global Variable
<script>
var userName = "Sohan";
function modifyUserName() {
userName = "Shyam";
};
function showUserName() {
alert(userName);
};
alert(userName); // display Sohan
modifyUserName();
showUserName();// display Shyam </script>

In the above example, the variable userName becomes a global variable because it is declared outside of any function. A modifyUserName() function modifies userName as userName is a global variable and can be accessed inside any function. The same way, showUserName() function displays current value of userName variable. Changing value of global variable in any function will reflect throughout the program.

Please note that variables declared inside a function without var keyword also become global variables.

Example of Global Variable
<script>
function createUserName() {
userName = "Sohan";
}

function modifyUserName()
{ if(userName)
userName = "Shyam";
};

function showUserName() {
alert(userName);
}
createUserName();
showUserName(); // Sohan
modifyUserName();
showUserName(); // Shyam </script>

In the above example, variable userName is declared without var keyword inside createUserName(), so it becomes global variable automatically after calling createUserName() for the first time.

A userName variable will become global variable only after createUserName() is called at least once. Calling showUserName() before createUserName() will throw an exception "userName is not defined".


Local Scope

Variables declared inside any function with var keyword are called local variables. Local variables cannot be accessed or modified outside the function declaration.

Example of local scope:
<script>

function createUserName() {
var userName = "Suresh";
}

function showUserName() {
alert(userName);
}

createUserName();
showUserName(); // throws error: userName is not defined </script>

In the above example, userName is local to createUserName() function. It cannot be accessed in showUserName() function or any other functions. It will throw an error if you try to access a variable which is not in the local or global scope. Use try catch block for exception handling.

Some tips.. If local variable and global variable have same name then changing value of one variable does not affect on the value of another variable.

Example of Scope
var userName = "Brinjay";
function ShowUserName()
{ var userName = "Suresh";
alert(userName); // "Suresh"
}
ShowUserName();
alert(userName); // Brinjay

JavaScript does not allow block level scope inside . For example, variables defined in if block can be accessed outside if block, inside a function.


Points to Remember:

  • Global variables can be accessed and modified anywhere in the program.
  • Local variables cannot be accessed outside the function declaration.
  • Global variable and local variable can have same name without affecting each other.
  • JavaScript does not allow block level scope inside brackets.
  • JavaScript has global scope and local scope.
  • Variables declared and initialized outside any function become global variables.
  • Variables declared and initialized inside function becomes local variables to that function.
  • Variables declared without var keyword inside any function becomes global variables automatically.