JavaScript provides multiple ways to declare variables (used to store values of any datatype), like var, let, and const. Now, let and const are used most in JavaScript, but understanding the var keyword is still important. In this blog, we will explore what var is, how it works, and when you should use it.
Table of Contents:
What is var in JavaScript?
var is the oldest keyword for declaring variables in JavaScript. It has been there from the time when language came, and this is the only way to declare the variables in JavaScript until ES6 introduced (let and const)
Example:
Output:
Explanation: In this example, a variable named name is created with the value “Intellipaat”.
Kickstart your Career in Tech With Hands-On Web Development Training
Start Your Web Development Journey Today!
Characteristics of the var Keyword
Here are the following unique characteristics of the var keyword in JavaScript that are important to know, whether you are a beginner or experienced in JavaScript:
1. Function Scope
Variables that are declared with the var keyword are function-scoped. This means they are only accessible inside the function in which they are defined.
Example:
Output:
Explanation: This example clearly explained to you that variables declared using the var keyword are function-scoped. x is declared inside the function, it is not accessible outside of it.
2. Hoisting
One of the most important and confusing aspects of var is Hoisting (accessing value before declaration). When you declare a variable using var, JavaScript moves the declaration to the top of its scope before execution.
Example:
Output:
Explanation: In this example, variable a is declared after the first console.log(a) statement. JavaScript hoisted the declaration. Thus, this is the reason why the first console.log() statement doesn’t throw an error but prints undefined.
3. No Block Scope
Unlike let and const, the var keyword doesn’t have block scope. This means that if you declare a variable using the var keyword inside a block ( {} ), it is also accessible outside the block.
Example:
Output:
Explanation: In this example, variable x is declared inside the if-block. It is still accessible outside of it. And this behaviour of JavaScript sometimes produces unexpected bugs in your code.
4. Can be Redeclared
Unlike let and const, the var keyword allows you to update and redeclare the same variable within the same scope without giving you any error.
Example:
Output:
Explanation: The var keyword gives you the flexibility to change its value whenever you want to change it. This flexibility sometimes produces an error in your code.
Difference between var, let, and const
Here is a quick comparison between the var, let, and const keywords in JavaScript that are used to declare variables in JavaScript.
Attributes |
var |
let |
const |
Scope |
Functional Scope |
Block Scope |
Block Scope |
Update/Redeclaration |
Can be updated and redeclared within the same scope |
Can be updated but not redeclared within the scope |
Cannot be updated and redeclared within the scope |
Declaration without initialization |
It can be declared without initialization |
Can be declared without initialization |
It cannot be declared without initialization |
Access without initialization |
Accessible, because the default value is undefined |
Inaccessible without initialization |
Inaccessible without initialization |
Hoisting |
Hoisted and initialized with default value |
Hoisted but not initialized |
Hoisted but not initialized |
When Should You Use var?
The var is generally not recommended in modern JavaScript. However, you can use it in some cases:
- If you’re maintaining an older codebase that depends on var, it may be best to continue using the var keyword.
- Having an idea of var helps you debug and understand older JavaScript code.
- In some cases, you may want to intentionally declare a variable in the global scope, and var allows this. But this is not good practice.
- The var is not block-scoped, thus, it creates some issues when used inside loops.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
The traditional way to declare variables in JavaScript is by using the var keyword. It has several limitations that make it less suitable for use in modern development projects. The var keyword has now been replaced by let and const (ES6). However, understanding the var keyword is still valuable, especially when you are working with older JavaScript code.
The var Keyword in JavaScript – FAQs
Q1. What is the purpose of the var keyword?
The var keyword in JavaScript is used to declare variables and variables are nothing just the storage containers which is used to store values.
Q2. Why was var replaced by let and const?
var was replaced because it has issues like function scoping, hoisting, and a lack of block scope, which led to errors in code.
Q3. Does var get hoisted like let and const?
Yes, but with a big difference – var is initialized as undefined, while let and const remain uninitialized until they are declared.
Q4. Can I use var inside an if statement?
Yes, but the variable will not be block-scoped, meaning it will be accessible outside the if block.
Q5. How to declare a variable in JavaScript?
To declare a variable in JavaScript, you can use the var keyword, which is the traditional method. Modern JavaScript uses let and const to declare variables.