Last modified: Jul 17, 2026
JS var vs let vs const Explained
JavaScript has three ways to declare variables: var, let, and const. Each behaves differently. Understanding these differences helps you write cleaner, safer code.
In the old days, var was the only option. But ES6 introduced let and const. These new keywords solve many problems with var. This guide explains everything you need to know.
What is var?
var is the oldest way to declare a variable. It has function scope, not block scope. This means a var variable is available anywhere inside the function where it is declared.
var also allows re-declaration. You can declare the same variable twice without an error. This can cause bugs in large codebases.
Another feature of var is hoisting. The declaration is moved to the top of its scope. But the initialization stays in place. So you can use a variable before it is defined, but its value will be undefined.
// Example of var hoisting
console.log(myVar); // Output: undefined
var myVar = 10;
console.log(myVar); // Output: 10
undefined
10
What is let?
let was introduced in ES6. It solves many issues with var. let has block scope. This means it only exists inside the block where it is defined, like inside an if statement or a loop.
You cannot re-declare a let variable in the same scope. This prevents accidental overwrites. However, you can still update its value.
let is hoisted but not initialized. If you try to use a let variable before its declaration, you get a ReferenceError. This is called the "temporal dead zone."
// Example of let block scope
if (true) {
let blockVar = "I am inside the block";
console.log(blockVar); // Output: I am inside the block
}
// console.log(blockVar); // Error: blockVar is not defined
I am inside the block
What is const?
const is also from ES6. It stands for constant. Once you assign a value to a const variable, you cannot reassign it. This makes your code more predictable.
const has the same block scope as let. It also has the temporal dead zone. You must initialize a const variable when you declare it.
Note: const does not make the value immutable. If the value is an object or array, you can still change its properties or elements. Only the binding is constant.
// Example of const
const pi = 3.14159;
// pi = 3; // Error: Assignment to constant variable
const person = { name: "Alice" };
person.name = "Bob"; // This is allowed
console.log(person.name); // Output: Bob
Bob
Key Differences Between var, let, and const
Here is a quick comparison. Scope: var is function-scoped. let and const are block-scoped. Re-declaration: var allows it. let and const do not. Reassignment: var and let allow it. const does not. Hoisting: All three are hoisted, but let and const are not initialized.
Use const by default. Only use let when you need to reassign a variable. Avoid var in modern code. This pattern reduces bugs and makes code easier to read.
For a deeper look at how variables behave in different situations, check out our JavaScript Variable Scope Explained guide.
Best Practices for Variable Declaration
Always declare variables before using them. This avoids hoisting confusion. Use const for values that should not change. Use let for loop counters or values that update.
Never use var in new projects. It is outdated and causes unexpected behavior. Many linters will warn you about var usage.
When working with functions, prefer const for function expressions. This ensures the function reference does not change accidentally.
To see practical examples of variable usage, read our JavaScript Variables Examples article.
Common Mistakes and How to Avoid Them
One common mistake is using var inside a loop. Because var is function-scoped, the same variable is shared across all iterations. This can cause unexpected results with asynchronous code.
Another mistake is forgetting that const allows property changes. Developers sometimes assume a const object is completely immutable. That is not true.
Always initialize const variables. Leaving them uninitialized will throw a syntax error. This is a good thing because it forces you to think about the starting value.
For a complete overview of variable types, see our JavaScript Variable Types Guide.
When to Use Each Keyword
Use const for most variables. It makes your intentions clear. Use let only when you know the value will change. Avoid var entirely in modern JavaScript.
In for loops, use let for the counter. This creates a new binding for each iteration. This is especially important with closures.
For global constants like API endpoints, use const. This prevents accidental overwrites from other scripts.
Conclusion
Understanding var, let, and const is essential for every JavaScript developer. var is old and bug-prone. let and const are modern and safer.
Use const as your default. Use let when you need reassignment. Forget var exists. This simple rule will improve your code quality.
Always consider scope and hoisting when writing code. Practice with examples to build confidence. Happy coding!