Open E-School

Friday, February 24, 1984

var or without var - How to declare a variable in javascript

Category: Programming Date: Thursday, March 26, 2015


Learn how to declare variables in javascript. Should we declare variable using var or without using var.

Declaring variables with var

When you declare a variable by using var in javascript then you are creating a variable within a given scope. If you are declaring a variable with var inside a function then the scope of the variable will be the function scope. If you are declaring a variable with var outside the body of any function then you are declaring a global variable.


var globalVariable;    function Test()  {      var localVariable;  }


The globalVariable will be treated by javascript as global. If suppose there was a variable in the global scope with the same name then that variable will be lost and the variable with same name will be re-declared.
The localVariable inside the function body has a local scope.

Declaring variables without var

If you declare a variable without using var then it will be treated as a global variable, no matter where you declare it. Javascript will go up the scope hierarchy and try to find a variable declaration with the same name. If it finds one then it will consider your variable as the pre-declared variable and give it a new value. If it is not able to find a variable with the same name in the whole hierarchy then it will think of it as a new global variable and attach it to global scope.


var gv_1;  gv_2 = 10;    function Test()  {         gv_2 = 20;           gv_3 = 30;           alert(gv_2);  }    Test();  alert(gv_3);

Code explanation

Look at the above code very carefully or you will miss the important point. We have declared three global variables in this code – gv_1, gv_2, gv_3. Out of them gv_1 has been declared using var and other two have been declared without using var. gv_2 has been declared in the global scope whereas gv_3 has been declared in the local scope of the Test function. We have given values 20 and 30 to gv_2 and gv_3 inside the Test function.

Now look at the alert statements carefully. The first alert statement we have called inside the Test function and the second alert statement we have called after calling the Test functions.

Note : If you do not call the Test function outside then the global variable gv_3 will not be created and your program will throw an error.

Now when the alert statements will execute you will see the value of gv_2 and gv_3. The variable gv_3 was created inside the Test function without using var and still it acts like a global variable.


You should always create variables using var in javascript. This will help you create local or global variables knowingly.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp