Var vs Let vs Const: Which to Use in JS?

Var vs Let vs Const: What to Use in Modern JavaScript?

var vs let vs const In modern JavaScript, use const by default, let when the value will change, and avoid var because of function scoping and hoisting pitfalls. This approach keeps your code predictable, reduces bugs, and aligns with today’s best practices in ES6+ development.

If you’re working with JavaScript regularly, you’ve definitely run into var, let, and const. They *look* similar, but behave quite differently. And trust me—understanding those differences saves you from some really annoying bugs. I learned this the hard way during a late-night debugging session where a rogue var ruined my weekend. Fun times.

What We Will Cover

  • Scope differences between var, let, and const
  • Hoisting & the “temporal dead zone”
  • Reassignment and immutability myths
  • Best practices developers actually use today
  • FAQ to clear common confusion

Scope Differences

One of the biggest differences is scope. var is function-scoped, while let and const are block-scoped.


function example() {
  if (true) {
    var x = 10;
    let y = 20;
  }

  console.log(x); // 10
  console.log(y); // ReferenceError: y is not defined
}
example();

Here’s the deal: x leaks outside the block because var doesn’t respect block scope. let does. Easy to remember, yet easy to forget—especially when you’re juggling deadlines.

Hoisting in JavaScript

Here’s a classic gotcha. Both var and let are hoisted—but not in the same way. let and const live in the Temporal Dead Zone until initialized.


console.log(a); // undefined
var a = 10;

console.log(b); // ReferenceError
let b = 20;

One time, during a code review, a teammate asked me why a variable was “undefined but still existed.” That’s hoisting + var. It works, but it’s confusing when reading code later. And clean code is everything, no?

Reassignment & Immutability

Important note: const prevents reassignment—not mutation. A lot of juniors confuse the two, understandably.


const person = { name: "Dinesh" };
person.name = "Kiran"; // works
person = {}; // TypeError: Assignment to constant variable

Meaning: const protects the binding, not the value. Small detail, big impact.

When to Use What?

  • Use const by default — most values don’t need reassignment
  • Use let only when you expect the value to change (loops, counters, etc.)
  • Avoid var completely — unless maintaining legacy code

Quick Example


const BASE_URL = "https://api.example.com";
let retries = 0;

function connect() {
  retries++;
  console.log("Retry:", retries);
}

Summary

Modern JavaScript encourages clarity. const gives your code stability. let gives controlled flexibility. var introduces risks—not worth it unless you’re dealing with old codebases.


FAQ

1. Is const always better?

Not always, but it’s the default choice. Use let when you truly need reassignment.

2. Should I ever use var in new code?

No. There’s no modern use-case for var except maintaining legacy code.

3. Does const make objects immutable?

No. It prevents reassignment, not mutation.

4. Why does let give ReferenceError during hoisting?

Because it stays in the Temporal Dead Zone until initialized.

5. Which is faster: var, let, or const?

No meaningful performance difference in real-world apps. Clarity matters more.

Leave a Comment

Your email address will not be published. Required fields are marked *