
Letβs dive into Variables in JavaScript step by step.
π¦ Variables in JavaScript
A variable is a container for storing data (values).
π Think of it as a labeled box where you can keep information.
1οΈβ£ Declaring Variables
In JavaScript, you can declare variables using var
, let
, and const
.
πΉ var
(Old way)
- Function-scoped (not block-scoped).
- Can be redeclared and updated.
- Not recommended in modern JS.
var name = "Raj";
var name = "Kumar"; // Redeclaration allowed
console.log(name); // Kumar
πΉ let
(Modern way)
- Block-scoped (exists only inside
{ }
). - Can be updated, but not redeclared in the same scope.
let age = 20;
age = 21; // β
allowed
// let age = 22; // β Error: cannot redeclare
console.log(age);
πΉ const
(Constant)
- Block-scoped.
- Cannot be redeclared or reassigned.
- Must be assigned at the time of declaration.
const PI = 3.14;
// PI = 3.15; β Error (cannot reassign)
console.log(PI);
2οΈβ£ Rules for Naming Variables (Identifiers)
β Allowed:
- Letters, digits,
_
,$
- Must start with a letter,
_
, or$
(not a number) - Case-sensitive (
myVar
βmyvar
)
β Not allowed:
let 1name = "Raj"; // β Invalid
let my-name = "Raj"; // β Invalid
let myName = "Raj"; // β
Valid (camelCase recommended)
3οΈβ£ Types of Values Variables Can Hold
JavaScript variables can store different data types:
let str = "Hello"; // string
let num = 42; // number
let isActive = true; // boolean
let x; // undefined
let y = null; // null
let arr = [1, 2, 3]; // array (object)
let obj = {name: "Raj"}; // object
4οΈβ£ Variable Scope
- Global Scope β Accessible everywhere.
- Local/Function Scope β Accessible only inside the function.
- Block Scope (
let
&const
) β Accessible only inside{ }
.
{
let a = 10;
var b = 20;
}
console.log(b); // β
Works (var is function-scoped)
// console.log(a); β Error (let is block-scoped)
5οΈβ£ Hoisting (Important)
var
is hoisted (moved to the top) but initialized asundefined
.let
andconst
are also hoisted but in a βtemporal dead zoneβ (not accessible before declaration).
console.log(x); // undefined (hoisted)
var x = 5;
console.log(y); // β Error (cannot access before initialization)
let y = 10;
β Summary
var
β old, function-scoped, can redeclare.let
β modern, block-scoped, can update but not redeclare.const
β modern, block-scoped, cannot redeclare or reassign.- Variables can store any type (string, number, object, etc.).
- Variable names are case-sensitive and must follow rules.
Perfect π Hereβs a clear comparison table of var
, let
, and const
in JavaScript:
π Difference Between var
, let
, and const
Feature | var | let | const |
---|---|---|---|
Scope | Function-scoped | Block-scoped ({ } ) | Block-scoped ({ } ) |
Redeclaration | β Allowed | β Not allowed | β Not allowed |
Reassignment | β Allowed | β Allowed | β Not allowed |
Default Initialization | undefined | β Not initialized (TDZ error) | β Not initialized (TDZ error) |
Hoisting | β
Hoisted (initialized as undefined ) | β Hoisted (but in Temporal Dead Zone) | β Hoisted (but in Temporal Dead Zone) |
Use in Loops | Not safe (because no block scope) | Safe | Safe |
Best Use Case | Legacy code, older browsers | General-purpose variables that may change | Constants that should never change |
πΉ Examples
var
var x = 10;
var x = 20; // β
Redeclaration allowed
console.log(x); // 20
let
let y = 30;
// let y = 40; // β Error: redeclaration not allowed
y = 40; // β
Reassignment allowed
console.log(y); // 40
const
const PI = 3.14;
// PI = 3.1415; // β Error: cannot reassign
console.log(PI); // 3.14
β In modern JavaScript:
- Use
let
for variables that can change. - Use
const
for constants or values that shouldnβt change. - Avoid
var
(only for legacy/old code).