Reply To: What’s the actual difference between let, const, and var in JavaScript?

Home › Forums › Community › What’s the actual difference between let, const, and var in JavaScript? › Reply To: What’s the actual difference between let, const, and var in JavaScript?

#299
Uncle John
Participant

    Alright kid, pull up a chair. Uncle John’s gonna sort you out.

    Twenty years I’ve been doing this. I remember when var was the only game in town, and let me tell you, we walked uphill both ways in the snow with that thing. So here’s the straight talk, no Medium article nonsense.

    The real difference, in order of what actually bites you:

    **1. Scope — this is the big one.**

    var is function-scoped. let and const are block-scoped. Block means anything inside { } — an if, a for loop, whatever.

    `js
    if (true) {
    var x = 1;
    let y = 2;
    }

    console.log(x); // 1 — leaked out, sneaky little bugger
    console.log(y); // ReferenceError — stays where it belongs
    `

    That var leaking out of blocks caused me more 3am debugging sessions in the 2000s than I care to count. Especially in loops. Ask any old-timer about closures in for loops with var — we still twitch.

    2. const does NOT mean “constant value.”

    It means **”constant binding.”**

    This trips up every beginner. You can’t *reassign* a const, but if it’s an object or array, you can absolutely mutate the guts out of it:

    `js
    const user = { name: “Alex” };

    user.name = “John”; // totally fine
    user = {}; // boom, TypeError
    `

    So no, let and const are NOT the same thing. Anyone who told you that is wrong.

    3. Hoisting and the “temporal dead zone.”

    var gets hoisted and initialized as undefined. So you can reference it before the line it’s declared and get undefined instead of an error — which hides bugs.

    let and const are hoisted too, but they sit in a “temporal dead zone” until the declaration runs. Touch them early and you get a clean ReferenceError. That’s a *feature*. Errors are your friends. Silent undefined is not.

    4. Reassignment.

    * var — reassign all day
    * let — reassign all day
    * const — nope, one shot

    So what do I actually reach for in real projects?

    const by default. Every time. If I genuinely need to reassign — a counter, a loop variable, a value that changes — I use let.

    I haven’t written var in a production codebase in probably eight years. It’s not “dead” in the sense that browsers still run it and you’ll see it in old code, but there’s zero reason to *choose* it in new code. It only has downsides.

    Some teams enforce this with a linter rule (prefer-const, no-var). That’s the norm now.

    **The one-liner to remember:**

    > Use const until you can’t, then use let. Forget var exists unless you’re reading legacy code.

    Don’t overthink it, kid. Everyone arguing about this online is mostly arguing about edge cases you won’t hit for another two years. Write const, switch to let when the compiler yells at you, and go build something.

    Now go outside and get some sun. Uncle John’s got code to review.

    — John

    • This reply was modified 17 hours, 56 minutes ago by Uncle John.
    • This reply was modified 17 hours, 56 minutes ago by Uncle John.
    • This reply was modified 17 hours, 55 minutes ago by Uncle John.