Difference between var, let & const in JS

Difference between var, let & const in JS

In JavaScript, variable declaration is not merely syntactic—it defines scope boundaries, mutability guarantees, and runtime behavior under hoisting. The distinction between var, let, and const is therefore foundational to writing predictable code.


1. var — Function Scope, Legacy Semantics

var is function-scoped, not block-scoped. Its declaration is hoisted and initialized with undefined, making it accessible before its actual definition.

function example() {
  console.log(x); // undefined (not error)
  var x = 10;
}
        

This behavior leads to scope leakage:

if (true) {
  var a = 5;
}
console.log(a); // 5 (escapes block)
        

Additionally, var permits re-declaration within the same scope, increasing the risk of unintended overrides.

var x = 10;
var x = 20; // valid, but unsafe
        

Conclusion: var reflects older design decisions and is unsuitable for modern, maintainable systems.


2. let — Block Scope with Controlled Mutability

let introduces block-level scoping, aligning variable visibility with logical code boundaries.

if (true) {
  let a = 5;
}
console.log(a); // ReferenceError
        

Although let is hoisted, it resides in the Temporal Dead Zone (TDZ) until initialization, preventing premature access:

console.log(x); // ReferenceError
let x = 10;
        

Re-declaration within the same scope is disallowed, enforcing stricter discipline:

let x = 10;
// let x = 20; // SyntaxError
        

However, reassignment is permitted, making it suitable for evolving state.


3. const — Immutable Binding, Not Immutable Value

const also provides block scope and participates in the TDZ. It must be initialized at declaration and cannot be reassigned thereafter.

const x = 10;
// x = 20; // TypeError
        

Crucially, const enforces immutability of the binding, not the underlying value:

const obj = { name: "A" };
obj.name = "B"; // valid (mutation allowed)
// obj = {};    // invalid (reassignment)
        

Thus, const guarantees reference stability, not deep immutability.


4. Guideline

  • Default to const to enforce immutability at the binding level.
  • Use let only when reassignment is logically required.
  • Eliminate var from modern codebases; its semantics are error-prone and obsolete.


To view or add a comment, sign in

More articles by Swastik Samal

  • Static and Non-Static Control Flow

    In Java, program execution follows a deterministic order. When a class contains both static and non-static members, two…

  • Factory Method and Singleton Class

    ✅ Factory Method Pattern Definition The Factory Method Pattern is a creational design pattern that provides an…

  • Constructors in Java

    In Java, a constructor is a special method used to initialize objects when they are created. 🔹 What is a Constructor?…

  • Types of Functions in JS

    1️⃣ Function Declaration Definition: A named function defined using the function keyword. Key Points Fully hoisted Can…

  • 📘Interfaces in Java

    1. What is an Interface? An interface is a blueprint of a class.

  • 📘Datatypes in JS

    JavaScript has 8 built-in data types, divided into Primitive and Non-Primitive (Reference). ✅ Primitive Data Types…

  • 📘Modifiers in Java

    Modifiers are reserved keywords in Java used to control the accessibility, visibility, and behavior of classes…

  • JS Token

    Tokens are the smallest meaningful units the JS engine understands. Before execution, your code is broken into these…

  • Method Overloading vs Method Overriding vs Method Hiding

    1️⃣ Method Overloading Compile-time polymorphism What it is Same method name, different parameter list, in the same…

  • 📘 Encapsulation & Access Modifiers

    1️⃣ What is Encapsulation? Encapsulation is the process of binding data (variables) and methods together and…

Explore content categories