JavaScript Variables: var, let and const
Learn how to declare variables in JavaScript using var, let, and const. Understand reassignment, scope, and the differences between these keywords with practical examples.
1. What Is a Variable?
A variable is a named identifier used to store or refer to a value in a program. We can use variables to store names, numbers, prices, results, and other application data.
JavaScript provides three common keywords for declaring variables:
var, let, and const.
Example
let studentName = "Samadhan";
let age = 22;
console.log(studentName);
console.log(age);
Output
Samadhan
22
Explanation:
The variable studentName stores a string, while
age stores a number. The console.log()
function displays their values.
2. Variable Declaration Diagram
A variable declaration introduces a variable name. An assignment provides a value to that variable.
Declaration Keyword
let, const, or var
Variable Name
studentName
Assigned Value
"Samadhan"
Complete Statement
let studentName = "Samadhan";
Here, let declares the variable,
studentName is the variable name, and
"Samadhan" is the assigned value.
3. What Is var?
The var keyword is used to declare a variable.
A var variable is function-scoped or globally scoped
when declared outside a function. It is not limited to a block
such as an if statement or loop.
Example
var course = "JavaScript";
console.log(course);
course = "Node.js";
console.log(course);
Output
JavaScript
Node.js
Explanation:
We first assign "JavaScript" to the variable.
Later, we assign "Node.js" to the same variable.
Reassignment is allowed with var.
4. What Is let?
The let keyword declares a block-scoped variable.
Its value can be reassigned, but the same variable cannot be
declared again in the same scope.
Example
let marks = 70;
console.log(marks);
marks = 85;
console.log(marks);
Output
70
85
Explanation:
The variable marks initially stores 70.
We then update it to 85. Since let allows reassignment,
the program runs successfully.
Block Scope Example
if (true) {
let message = "Inside the block";
console.log(message);
}
// message is not accessible here
Output
Inside the block
The variable message is accessible inside the
if block only. Trying to access it outside the block
would cause a ReferenceError.
5. What Is const?
The const keyword declares a block-scoped binding
that must be initialized when declared. The binding cannot be
reassigned later.
Example
const instituteName = "CIIT Training Institute";
console.log(instituteName);
Output
CIIT Training Institute
Explanation: The variable is initialized when declared. We can read its value, but we cannot assign a different value to the same binding.
Invalid Example
const course = "JavaScript";
course = "Node.js";
Result
TypeError: Assignment to constant variable.
Note:
A const object or array can still have its contents
modified. The restriction applies to reassignment of the binding,
not automatic immutability of the object.
6. Difference Between var, let and const
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function or global | Block | Block |
| Reassignment | Allowed | Allowed | Not allowed |
| Redeclaration in the same scope | Allowed | Not allowed | Not allowed |
| Must initialize at declaration | No | No | Yes |
| Recommended for new code | Generally avoid | When reassignment is needed | When reassignment is not needed |
7. Understanding Variable Scope
Scope determines where a variable can be accessed in a program. The following example demonstrates the difference between function scope and block scope.
Function Scope
var is accessible throughout its containing function.
Block Scope
let and const are accessible within their declaring block.
Example
if (true) {
var first = "var variable";
let second = "let variable";
console.log(first);
console.log(second);
}
console.log(first);
// second is not accessible outside the block
Output
var variable
let variable
var variable
The var variable remains accessible outside the
if block because it is not block-scoped.
The let variable is limited to the block.
Uncommenting a statement that accesses second
outside the block would cause a ReferenceError.
8. Best Practices
-
Prefer
constwhen a variable binding does not need to be reassigned. -
Use
letwhen the variable needs reassignment. -
Avoid using
varin new code unless its behavior is specifically required. -
Choose meaningful names such as
studentNameandtotalMarks. -
Remember that
constdoes not make an object or array immutable.
9. Practice Task
Student Information Program
Write a JavaScript program that performs the following tasks:
- Declare a student name using
const. - Declare marks using
let. - Display the student name and marks.
- Update the marks and display the new value.
- Create an
ifblock and declare a variable usinglet. - Observe which variables can be accessed outside the block.
var, let,
and const, then explain their differences.
Important Points
varis function-scoped or globally scoped.letis block-scoped and supports reassignment.constis block-scoped and does not allow reassignment.- A
constdeclaration must be initialized. - A
letorconstdeclaration cannot be redeclared in the same scope. - Prefer
constby default and useletwhen reassignment is required.
Summary
JavaScript provides var, let, and const to declare variables. The var keyword is function-scoped or globally scoped, while let and const are block-scoped. We can reassign variables declared with var and let, but a const binding cannot be reassigned. Understanding scope and reassignment helps us write predictable, readable JavaScript programs.