Variables — var, let, const
Variables are named containers that store data values. JavaScript has three
ways to declare them — var, let, and const — each with
different behaviours around scope, hoisting, and reassignment.
What is a Variable?
A variable is a named memory location that stores a value.
Think of it as a labelled box — you put data in, refer to it by name, and can update its contents
later (unless it's const).
// Declaring a variable
let name; // declared but undefined
name = "Alice"; // assigned a value
// Declare and assign in one step
let age = 25;
const PI = 3.14159;
// Using the variables
console.log(name); // "Alice"
console.log(age); // 25
console.log(PI); // 3.14159
// Reassigning
age = 26; // ✅ let can be reassigned
// PI = 3.14; // ❌ TypeError — const cannot be reassigned
var, let, and const — Overview
Modern JavaScript has three declaration keywords. Understanding their differences is one of the most important foundations in JS.
- Function-scoped (or global)
- Hoisted to top of function
- Can be re-declared
- Can be reassigned
- ⚠ Avoid in modern code
- Block-scoped
- Not accessible before declaration (TDZ)
- Cannot be re-declared in same scope
- Can be reassigned
-
Use for changing values
- Block-scoped
- Not accessible before declaration (TDZ)
- Cannot be re-declared
- Cannot be reassigned
-
Use by default — always
Side-by-Side Comparison
| Property | var | let | const |
|---|---|---|---|
| Introduced | ES1 (1997) | ES6 (2015) | ES6 (2015) |
| Scope | Function / Global | Block | Block |
| Hoisted? | ✓ Yes (undefined) | ⚠ Yes (TDZ) | ⚠ Yes (TDZ) |
| Re-declarable? | ✓ Yes | ✗ No | ✗ No |
| Reassignable? | ✓ Yes | ✓ Yes | ✗ No |
| Must initialise? | ✗ No | ✗ No | ✓ Yes |
| Recommended? | ✗ Avoid | ⚠ When needed | ✓ Default choice |
Scope — Where Can You Access a Variable?
Scope is the region of code where a variable is accessible. JavaScript has three types of scope: global, function, and block.
let globalVar = "I'm global"; // accessible everywhere
function myFunction() {
let funcVar = "I'm function-scoped";
console.log(globalVar); // ✅ accessible
console.log(funcVar); // ✅ accessible
if (true) {
let blockVar = "I'm block-scoped";
var oldVar = "var ignores block scope";
console.log(blockVar); // ✅ accessible inside block
}
// console.log(blockVar); // ❌ ReferenceError — out of block
console.log(oldVar); // ✅ var leaks out of the block!
}
myFunction();
// console.log(funcVar); // ❌ ReferenceError — out of function
var is function-scoped, not block-scoped. So a var declared inside an if
block, for loop, or while
loop leaks out into the surrounding function. This is one of the main reasons var was superseded by let and
const.
Hoisting
Hoisting is JavaScript's behaviour of moving variable and function declarations to the top of their scope during the compilation phase — before any code runs. Only the declaration is hoisted, not the assignment.
var x = 5;
console.log(x); // undefined
x = 5; // assignment stays here
// var: hoisted as undefined
console.log(a); // undefined (no error)
var a = "hello";
console.log(a); // "hello"
// let: hoisted but in Temporal Dead Zone — accessing throws
// console.log(b); // ❌ ReferenceError: Cannot access 'b' before initialization
let b = "world";
console.log(b); // "world"
// const: same as let — TDZ applies
// console.log(c); // ❌ ReferenceError
const c = 42;
console.log(c); // 42
const with Objects and Arrays
const prevents the variable
binding from being reassigned — it does not make the value immutable. Objects
and arrays declared with const can still have their contents
modified.
const user = { name: "Alice", age: 25 };
// ✅ Mutating object properties is allowed
user.age = 26;
user.email = "alice@example.com";
console.log(user); // { name: "Alice", age: 26, email: "alice..." }
// ❌ Reassigning the variable itself is NOT allowed
// user = { name: "Bob" }; // TypeError!
const nums = [1, 2, 3];
nums.push(4); // ✅ mutating the array is fine
nums[0] = 99; // ✅ changing an index is fine
// nums = [5, 6, 7]; // ❌ reassigning is not allowed
Object.freeze(). But for most purposes, const is enough to signal "this reference should not change".
Best Practices
const for every
variable. This signals intent — "this value should not change" — and prevents accidental
reassignment.let when you
know the variable will be reassigned — loop counters (for), accumulators, or state flags.var in modern code. Its
function-scoping and hoisting behaviour are confusing. let and const replace
it entirely.let a = 1; not let a = 1, b = 2, c;. Much easier to read and debug.Try It Yourself
Experiment with variable declarations. Try changing const to let and reassigning,
or testing block scope.
What happens when you try to access a let
variable before it is declared?
- Use const by default — it cannot be reassigned. Use let only when reassignment is needed. Never use var.
varis function-scoped and leaks out of blocks.letandconstare block-scoped.varis hoisted asundefined.letandconstare hoisted but in the Temporal Dead Zone — accessing them before declaration throws a ReferenceError.constprevents the binding from changing, not the contents. Objects and arrays declared withconstcan still be mutated.- Declare one variable per line for readability and easier debugging.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.