Skip to main content

Command Palette

Search for a command to run...

Understanding the Differences Between let, const, and var in JavaScript

Updated
4 min read
Understanding the Differences Between let, const, and var in JavaScript
M

A self-motivated and enthusiastic web developer with a deep interest in JavaScript (React.js). To work in the Software industry with modern web technologies of different local & multinational Software/ IT agencies of Bangladesh and grow rapidly with increasing responsibilities.

Understanding the Differences Between let, const, and var in JavaScript

When writing modern JavaScript, choosing between let, const, and var is an essential part of ensuring your code is clean, predictable, and maintainable. But what are the differences between these three variable declaration keywords, and when should you use each? Let’s explore this in a Q&A format to make it easy to understand.


Q1: What are let, const, and var in JavaScript?

let, const, and var are keywords used to declare variables in JavaScript.

  • var: The oldest keyword, available since the beginning of JavaScript, but often avoided in modern code due to its quirks.

  • let: Introduced in ES6 (2015), it provides block scoping and better control over variables.

  • const: Also introduced in ES6, it’s used to declare variables that won’t be reassigned.


Q2: What is the scope of let, const, and var?

Scope Comparison

  1. var:

    • Function-scoped: The variable is accessible throughout the entire function it’s declared in.

    • If declared outside a function, it becomes globally scoped.

    function testVar() {
        if (true) {
            var x = 10; // Function scoped
        }
        console.log(x); // Output: 10
    }
  1. let and const:

    • Block-scoped: The variable is only accessible within the block {} where it’s declared.
    function testLetConst() {
        if (true) {
            let y = 20;
            const z = 30;
        }
        console.log(y); // ReferenceError
        console.log(z); // ReferenceError
    }

Q3: Can you redeclare variables with let, const, and var?

  1. var:
    Variables can be redeclared in the same scope without an error.

     var a = 10;
     var a = 20; // Allowed
     console.log(a); // Output: 20
    
  2. let and const:
    Redeclaring variables in the same scope results in a syntax error.

     let b = 10;
     let b = 20; // SyntaxError: Identifier 'b' has already been declared
    

Q4: Are these variables hoisted?

All three are hoisted to the top of their scope, but their behavior differs:

  1. var:
    Variables are hoisted and initialized as undefined. This allows accessing them before their declaration.

     console.log(c); // Output: undefined
     var c = 10;
    
  2. let and const:
    Variables are hoisted but remain in the Temporal Dead Zone (TDZ) until their declaration. Accessing them before the declaration results in a ReferenceError.

     console.log(d); // ReferenceError: Cannot access 'd' before initialization
     let d = 10;
    

Q5: Can you reassign values to these variables?

  1. var and let:
    Both allow reassignment.

     var e = 10;
     e = 20; // Allowed
     console.log(e); // Output: 20
    
     let f = 30;
     f = 40; // Allowed
     console.log(f); // Output: 40
    
  2. const:
    Reassignment is not allowed. Once declared, a const variable’s value cannot be changed.

     const g = 50;
     g = 60; // TypeError: Assignment to constant variable
    

    However, the contents of objects or arrays declared with const can still be modified.

     const obj = { key: 'value' };
     obj.key = 'newValue'; // Allowed
     console.log(obj); // Output: { key: 'newValue' }
    

Q6: Do these variables attach to the global object?

  1. var:
    Globally declared var variables become properties of the global object (window in browsers or global in Node.js).

     var h = 10;
     console.log(window.h); // Output: 10
    
  2. let and const:
    These variables do not attach to the global object.

     let i = 20;
     console.log(window.i); // Output: undefined
    

Q7: When should I use let, const, or var?

  • Use const for values that do not change (preferred in most cases).

      const apiUrl = "https://api.example.com";
    
  • Use let for variables that will change.

      let counter = 0;
      counter += 1;
    
  • Avoid var in modern JavaScript, as it can lead to bugs due to its function-scoped behavior.


Q8: What’s the performance difference between let, const, and var?

In modern JavaScript engines, there’s minimal performance difference between let, const, and var. The choice primarily impacts code readability and maintainability, not execution speed.


Summary Table

Featurevarletconst
ScopeFunctionBlockBlock
RedeclarationAllowedNot AllowedNot Allowed
HoistingYes (initialized undefined)Yes (TDZ applies)Yes (TDZ applies)
ReassignmentAllowedAllowedNot Allowed
Global Object PropertyYesNoNo

Final Thoughts

When working with JavaScript:

  • Prefer const whenever possible for variables that won’t change.

  • Use let for variables that will change during the program's execution.

  • Avoid using var unless you're maintaining older codebases.

Mastering let, const, and var will help you write cleaner, safer, and more predictable JavaScript code! 🚀

More from this blog

DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

অনেকেই বলে — “ভাই, Web Development-এ DSA লাগে না” আসলে Junior Developer থাকলে এমনটাই মনে হয়।আমি নিজেও Junior থাকতে তাই ভাবতাম। আমার আবার একটা বাজে স্বভাব আছে 😅👉 কোন কিছুর বাস্তব প্রয়োজন না বুঝলে সেটা আমার মাথায় ঢুকে না। Junior থাকতে DSA শিখেছি...

Dec 24, 20253 min read
DSA মানে শুধু LeetCode না — DSA মানে Production System slow না করা

🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Rust-এ Garbage Collector (GC) নেই, কিন্তু মেমরি নিরাপত্তা (memory safety) আর performance Rust-এর সবচেয়ে শক্তিশালী দিক।তাই, Go-এর মতো runtime-based GC না থাকা সত্ত্বেও Rust compile-time এ মেমরি ম্যানেজ করে Ownership System, Borrow Checker, আর Lifetime...

Oct 5, 202523 min read
🦀 Rust-এ মেমরি ম্যানেজমেন্ট: GC ছাড়া In-depth.

Morshedul Munna

45 posts

As a Software Developer, I am like an architect who designs and builds digital Products. I use my knowledge and expertise to create modern applications that are both efficient and elegant.