Tutorials JavaScript Variables — var, let, const
Ch 9 / 78
JavaScript Beginner Chapter 6

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 you'll learn in this chapter
Declare variables with var, let, and const
Understand function scope vs block scope
Understand hoisting and the Temporal Dead Zone
Know when to use let vs const
Avoid common pitfalls with var
Apply modern best practices for variables

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).

JS — declaring and using variables
// 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.

var
ES1 (1997) — Old
  • Function-scoped (or global)
  • Hoisted to top of function
  • Can be re-declared
  • Can be reassigned
  • ⚠ Avoid in modern code
let
ES6 (2015) — Modern
  • Block-scoped
  • Not accessible before declaration (TDZ)
  • Cannot be re-declared in same scope
  • Can be reassigned
  • Use for changing values
const
ES6 (2015) — Preferred
  • 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.

JS — scope demonstration
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 scope leak
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.

What you write
console.log(x); // ?
var x = 5;
What JS sees (after hoisting)
var x; // hoisted, value = undefined
console.log(x); // undefined
x = 5; // assignment stays here
JS — hoisting with var vs let/const
// 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.

JS — const with objects/arrays
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
Note
To make an object truly immutable (deep freeze), use Object.freeze(). But for most purposes, const is enough to signal "this reference should not change".

Best Practices

Use const by default
Start with const for every variable. This signals intent — "this value should not change" — and prevents accidental reassignment.
Switch to let when needed
Only switch to let when you know the variable will be reassigned — loop counters (for), accumulators, or state flags.
Never use var
Avoid var in modern code. Its function-scoping and hoisting behaviour are confusing. let and const replace it entirely.
One declaration per line
Declare each variable on its own line: let a = 1; not let a = 1, b = 2, c;. Much easier to read and debug.
⚡
Free Resource
Download our JavaScript Variables & Scope Cheat Sheet
var vs let vs const, scope diagrams, and hoisting examples in one page.
Get Sheet

Try It Yourself

Experiment with variable declarations. Try changing const to let and reassigning, or testing block scope.

Live Editor
✎ HTML / JS
👁 Preview
Quick Check — Chapter 6

What happens when you try to access a let variable before it is declared?

Chapter Summary
  • Use const by default — it cannot be reassigned. Use let only when reassignment is needed. Never use var.
  • var is function-scoped and leaks out of blocks. let and const are block-scoped.
  • var is hoisted as undefined. let and const are hoisted but in the Temporal Dead Zone — accessing them before declaration throws a ReferenceError.
  • const prevents the binding from changing, not the contents. Objects and arrays declared with const can still be mutated.
  • Declare one variable per line for readability and easier debugging.

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel