Learn the difference between wp_remote_get() and cURL in WordPress. Find out which is better for making HTTP requests in your plugins or themes.
Difference Between var, let and const in JavaScript
Table of Contents
- What is var in JavaScript?
- What is let in JavaScript?
- What is const in JavaScript?
- Key Differences Between var, let and const
- When to Use var, let, or const
- Conclusion
- FAQs
When writing JavaScript code, we use variables to store data. There are three main ways to declare variables:
varletconst
But what’s the difference between them? In this post, we’ll explain them simply, with examples and best practices, so you’ll know when to use which.
What is var in JavaScript?
The var keyword is the old way to declare variables in JavaScript. It has function scope, not block scope.
Example:
function example() {
console.log(a); // undefined
var a = 10;
console.log(a); // 10
}
example();
Features of var:
- Scoped to functions, not blocks.
- Can be redeclared.
- Can be reassigned.
- Hoisted (moved to the top), but initialized as
undefined.
What is let in JavaScript?
The let keyword was introduced in ES6 (2015). It is block-scoped, meaning it works only within the {} block where it’s declared.
Example:
let name = "John";
name = "Jane"; // ✅ allowed
let name = "Doe"; // ❌ SyntaxError: Identifier 'name' has already been declared
Features of let:
- Scoped to blocks (
if,for,{}). - Cannot be redeclared in the same scope.
- Can be updated.
- Hoisted, but not initialized — using before declaration gives ReferenceError.
What is const in JavaScript?
const is also introduced in ES6. It is used to declare constant values.
Example:
const PI = 3.14;
PI = 3.14159; // ❌ TypeError: Assignment to constant variable.
But remember: for objects and arrays, you can change the contents, just not reassign them.
Example:
const person = { name: "John" };
person.name = "Jane"; // ✅ allowed
person = {}; // ❌ TypeError
Features of const:
- Scoped to blocks.
- Cannot be redeclared or reassigned.
- Must be initialized at declaration.
Key Differences Between var, let and const
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Redeclare | ✅ Yes | ❌ No | ❌ No |
| Reassign | ✅ Yes | ✅ Yes | ❌ No |
| Hoisting | ✅ Yes (undefined) | ✅ Yes (no init) | ✅ Yes (no init) |
| ES6 Support | ❌ Old | ✅ Yes | ✅ Yes |
When to Use var, let or const
Use let when the value can change (like a counter, loop variable, etc.).
Use const when the value should not change (like configuration settings, fixed values).
Avoid var in modern JavaScript. It’s outdated and can cause confusing bugs due to its function-scoping.
Conclusion
Understanding the difference between var, let, and const is important for writing clean and modern JavaScript code.
Tip: Stick to let and const for better code and fewer errors.
FAQs
Can I use var and let in the same block?
You can, but it’s not recommended. Use let or const to avoid confusion.
Is const completely read-only?
You can’t reassign the variable, but you can change contents of arrays and objects.
Why is let better than var?
let respects block scope and avoids issues related to redeclaration and hoisting.
What happens if I forget to initialize a const variable?
You’ll get a SyntaxError — const must be initialized when declared.

This Post Has 0 Comments