JavaScript - var Keyword
JavaScript - var Keyword
Thevarkeyword in JavaScript is used to declare variables. Before ES6, there was onlyvarkeyword to declare a variable. In ES6, let and const keywords were introduced as a better way to declare variables. It is advised to use let instead of var to declare a variable. If you are targeting older version of browser, you can use var.
In JavaScript, variables are the data storage container. You can use the variables to store any type of data. For example, string, number, boolean, object, etc.
A variable declared with var keyword has function scope. Whereas a variable declared with let has block scope.
Syntax
Follow the syntax below to define the variable using the βvarβ keyword.
var identifier;
var identifier = value;Here, the identifier can be a valid variable name. In theJavaScript - Variableschapter, we have already discussed the rules to define valid identifiers.
You may assign or not value to the variable while declaring it using the JavaScript βvarβ keyword.
Example
In the below code, we have defined the 3 variables using the βvarβ keyword. In the num1 variable, we have stored the numeric value. In the str1 variable, we have stored the string value; in the bool variable, we have stored the boolean value.
Output
30
Hello World!
trueJavaScript Variable Scopes
The scope of the variable defined using the JavaScript βvarβ keyword has afunction scopeorglobal scope.
JavaScript Function Scope
When you define the variable inside the function using the var keyword, it can be accessible inside the function anywhere. Even if you define the variable using the var keyword inside the particular block inside the function, it can be accessible anywhere.
Letβs understand the function scope with the example below.
Example
In the below code, we have defined the variable βxβ inside the function. We have also defined the variable βyβ inside the block, which is inside the function.
After that, we access the variables x and y inside the function.
Output
X == 10
Y == 20If you define the variable using the let keyword inside the block, which is inside the function, and try to access it outside the block, it will throw an error.
JavaScript Global Scope
It becomes the global variable if you define the variable using the βvarβ keyword outside the function or a particular block.
After that, you can access the variable anywhere inside the code using the window object.
Example
We have defined the βnum1β global variable in the code below.
After that, we access the βnum1β variable inside the function using and without using the window object.
Output
num1 + num2 = 30
window.num1 + num2 = 30JavaScript Variable Hoisting
The variable defined using the βvarβ keyword is hoisted at the top of its scope. It means JavaScript adds the declaration of the variable at the top of its scope.
So you can access the variable before the declaration of the variable.
Example
In the below code, we have initialized the variable βaβ and printed its value before its declaration.
The code runs without error, as the variable defined using the var keyword is hoisted at the top of its scope.
Output
Value of the variable a is: 98The above JavaScript code is similar to the below code.
var a;
a = 98;
document.getElementById('output').innerHTML += a;Redeclaration of variable defined using the βvarβ keyword
You can redeclare the variables using the βvarβ keyword, and the code will run without any error.
If you have initialized the last duplicate variable with the latest value, it will contain that value. But if you donβt initialize the last duplicate variable, the variable will contain its previous value without losing it.
Example
In the code below, we have defined the variable βaβ two times and initialized it with different values. You can observe in the output that variable βaβ contains the last value.
We have defined the variable βaβ a third time and havenβt initialized it. So, it contains the value of the 2nd βaβ variable.
Output
The value of the a is: 20
The value of the a is: 20If you define the duplicate variables in the different scopes, it can have value according to where you access the variable.
Example
In the code below, we have defined the βnumβ variable outside and inside the function. When you access it inside or outside the function, it prints the different values.
Output
Num inside the function is: 20
Num outside the function is: 10Declaring Many Variables in a Single Statement
If you want to declare multiple variables using the JavaScript βvarβ keyword, you can declare all variables in a single statement. It makes code readable.
Example
In the below code, we have declared the variables βaβ and βbβ in the single statement and initialized them with different values after declaring.
Output
The value of a is: 70
The value of b is: 80However, you can also assign the values to the variables while declaring the multiple variables in a single statement.
Using the var keyword with loops
When you use the JavaScript βvarβ keyword to define the iterator variable of for loop, you can also access it outside the for loop.
Example
In the below code, we have defined the variable βpβ inside the for loop. We access the variable p inside and outside the loop.
Output
The value of p is: 0
The value of p is: 1
The value of p is: 2
The value of p is: 3
The value of p is: 4
The value of p outside the loop is: 5Declaration with Destructuring
In JavaScript, you can declare the variables using the βvarβ keyword while destructuring the array or objects.
Example
In the below code, the βarrβ array contains 3 variables.
After that, we define variables using the βvarβ keyword and destructure the array.
Output
a = 4, b = 5, c = 6You can also use the βvarβ keyword to define the variables and store the objects or function expressions.