JavaScript is a Dynamically Typed Language.
This means:
- The programmer does not need to specify the data type while declaring a variable.
- The datatype is assigned automatically at runtime based on the value stored in the variable.
Example:
let age = 25; // Number
let name = "Basha"; // String
let status = true; // BooleanJavaScript provides three keywords for declaring variables:
varletconst
The var keyword is used to declare variables in JavaScript.
- Function Scoped
- Can be re-declared
- Can be re-assigned
- Does not support block scope
- Older way of declaring variables
{
var firstName = "Apple";
console.log(firstName);
var firstName = "Banana";
console.log(firstName);
}
console.log(firstName);Apple
Banana
Banana
varallows re-declaration.- Variables declared with
varinside a block are accessible outside the block. - Therefore,
console.log(firstName)works outside the braces.
The let keyword is used to declare block-scoped variables.
- Block Scoped
- Cannot be re-declared in the same scope
- Can be re-assigned
- Introduced in ES6 (ECMAScript 2015)
{
let age = 26;
console.log(age);
age = 30;
console.log(age);
}
// console.log(age); // Error26
30
ReferenceError: age is not defined
letvariables exist only inside the block.- They can be reassigned.
- They cannot be accessed outside the block.
The const keyword is used to declare constants.
- Block Scoped
- Must be initialized while declaration
- Cannot be re-assigned
- Cannot be re-declared
{
const isMarried = true;
console.log(isMarried);
// isMarried = false; // Error
}
// console.log(isMarried); // Errortrue
TypeError: Assignment to constant variable.
ReferenceError: isMarried is not defined
- A
constvariable cannot be reassigned. - It is accessible only inside the block.
- Attempting to change its value causes an error.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Variables in JavaScript</title>
<script src="demo.js"></script>
</head>
<body>
</body>
</html>// var keyword
{
var firstName = "Apple";
console.log(firstName);
var firstName = "Banana";
console.log(firstName);
}
console.log(firstName);
// let keyword
{
let age = 26;
console.log(age);
age = 30;
console.log(age);
}
// console.log(age);
// const keyword
{
const isMarried = true;
console.log(isMarried);
// isMarried = false;
}
// console.log(isMarried);| Feature | var | let | const |
|---|---|---|---|
| Scope | Function Scope | Block Scope | Block Scope |
| Re-declare | Yes | No | No |
| Re-assign | Yes | Yes | No |
| Hoisting | Yes | Yes (TDZ) | Yes (TDZ) |
| Introduced In | ES5 | ES6 | ES6 |
- JavaScript is a Dynamically Typed Language.
varis function scoped.letis block scoped.constis block scoped and immutable.- Modern JavaScript development prefers
letandconst. - Use
constby default andletonly when reassignment is required.
JavaScript variables are declared using var, let, and const.
var→ Old way, function scoped.let→ Block scoped and re-assignable.const→ Block scoped and cannot be re-assigned.
For modern JavaScript development, let and const are recommended because they provide better scope management and help avoid bugs.