Scope of Variables in JavaScript:

In any programming language scope of variables means where can be the variables accessed by the unction. In JavaScript variables have only two type of scope. They are as follows:

  • Global Variables
  • Local Variables

Global Variable:

To make a variable global you have to declare that variable outside the function so that the variable become a global variable and you can accesses that variable in any function of web page.

Example:

Local Variable:

The variables which are declared inside the function are known as local variables and you can use them only that function means the scope of those variables is local.

Example:

Note:

You can change the value of variable in run time if you already define it when you declare that variable.Following example will explain you that how can you change the value of variables in run time.

Example:

 

Leave a Reply

Your email address will not be published. Required fields are marked *