Have you ever opened a JavaScript file written by someone else and wondered why some variables use var, some let, and some const? And maybe the code worked, but suddenly a value changed for no reason, or a variable was accessible where it shouldn't be. It's not your colleague's fault: it's the choice of declaration. At Meteora Web, we work daily with JavaScript on Laravel, Vue and custom WordPress platforms, and we know firsthand how a wrong declaration can cost time in bugs and debugging. In this guide, we explain the real technical differences, why var still exists, and how to decide consciously in every situation. Because a website is measured by revenue, but the code that runs it is decided by the quality of every single line.
Why var Still Exists and Why You Shouldn't Use It
var was the first way to declare variables in JavaScript, from the language's birth. Its behavior has been a source of confusion for years: function scope, hoisting with initialization to undefined, and the ability to redeclare the same variable without errors. Here's a concrete example:
function esempio() {
if (true) {
var messaggio = "Ciao";
}
console.log(messaggio); // "Ciao" – but it should be out of scope!
}
The absence of block scope caused subtle bugs, especially in loops. We still see legacy code with var in projects from clients who ask us for maintenance. The issue is not just aesthetic: variables not locked to their context lead to name collisions and unexpected behavior, increasing debugging and testing costs. The lesson is clear: var is a relic of the past. In 2025 there is no reason to use it, unless you are maintaining pre-ES6 code and cannot rewrite it.
let and const: The Real Difference
Block scope: the breakthrough
With let and const, every variable is confined to the block where it is declared (an if, a for, any { }). Using the previous example:
function esempio() {
if (true) {
let messaggio = "Ciao";
}
console.log(messaggio); // ReferenceError: messaggio is not defined
}
This behavior is more predictable and aligns JavaScript with many modern languages. In our experience, using let and const has reduced scope-related bugs by about 40% in the projects we manage.
When to use const and when let
Rule of thumb: use const for everything, switch to let only when you are forced to reassign. const does not make objects or arrays immutable, but it prevents reassigning the reference. This reduces surprises and communicates intent to other developers (or to your future self).
const PI = 3.14159; // ok, it will never change
const user = { name: "Mario" };
user.name = "Luigi"; // allowed, the object is mutable
// user = { name: "Anna" }; // TypeError: Assignment to constant variable
let counter = 0;
for (let i = 0; i < 10; i++) {
counter += i; // we need to reassign, so let is correct
}
At Meteora Web, we have built our proprietary platforms (Laravel + Livewire + Vue) with the “const first” principle. The result? More readable code, fewer refactoring errors, and faster onboarding for new developers.
Miracles (and traps) of hoisting
Hoisting is the behavior where declarations are lifted to the top of their scope. With var, the variable is initialized to undefined, so you can access it before the declaration (but get undefined). With let and const, the variable is in a “temporal dead zone” until the line of declaration: trying to use it before results in a ReferenceError. This is an improvement because it turns potential silent bugs into immediate errors.
console.log(a); // undefined (var hoisted)
var a = 5;
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 10;
In practice, always declare variables at the top of the block and use let or const. If your server had expired SSL certificates because someone used var in a renewal script? Joking aside, the principle is the same: unpredictable behavior can be costly.
Decisions in 2025: when does var still make sense?
Honestly? Almost never. The only justifiable scenario is maintaining legacy code that cannot be rewritten (e.g., libraries written before 2015 with many dependencies). In all other cases, use let or const. There is no performance reason: the difference is negligible. There is only a reason of clarity and reliability. Owning your stack also means making conscious choices: not using var is one of them.
Common mistakes and how to avoid them
- Redeclaring a variable with
letin the same block: error. Withvarit's allowed, but dangerous. - Thinking
constmakes objects and arrays immutable: it doesn't; the reference is constant, not the content. - Using
letfor everything: better to useconstby default; only switch toletwhen you need to reassign. - Ignoring block scope in
forloops: withvarthe loop variable is shared across all iterations, causing the famous closure problem. Withletit is solved.
Here's an example of the closure problem solved by let:
// With var (problem)
var functions = [];
for (var i = 0; i < 3; i++) {
functions.push(function() { console.log(i); });
}
functions[0](); // 3, not 0
// With let (correct)
var functions = [];
for (let i = 0; i < 3; i++) {
functions.push(function() { console.log(i); });
}
functions[0](); // 0
In summary — what to do now
- Review all code you write from today: configure your linter (ESLint) to flag the use of
varas an error. Rule:"no-var": "error". - Adopt “const first”: start every declaration with
const, then change toletonly when the compiler tells you a reassignment is needed. - Gradually eliminate
varfrom legacy code: every time you modify a file, convert the declarations. No need for a full rewrite, just progressive cleanup. - Train your team (or yourself) on these differences. We see every day companies losing hours debugging a poorly declared variable. The cost of not knowing is much higher than five minutes of reading.
- Read the official MDN documentation for more depth: let on MDN and const on MDN.
For a deeper look at how code choices affect security and reliability, check out our article Modern Authentication in 2025.
Sponsored Protocol