JavaScript provides multiple ways to declare variables: var, let, and const. While let and var may seem similar at first glance, they have fundamental differences in scope, hoisting, and behavior in different contexts.
In this blog, we will explore the difference between let and var, understand when to use each, and see how they behave in different scopes and functions.

What is var in JavaScript?
The var keyword was traditionally used to declare variables in JavaScript. However, it comes with certain drawbacks, particularly related to function scope and hoisting.
Example of var
The var keyword was introduced just to declare variables in JavaScript. However, it comes with certain drawbacks, particularly related to function scope and hoisting.
// LogicalDuniya example of var -
console.log('==>'+x); // undefined
var x = 10;
console.log('==>'+x); // 10
if (true) {
var x = 20;
console.log('==>'+x); // 20
}
console.log('==>'+x); // 20 (Value changed globally)
Key Characteristics of var
- Variables declared with
varare function-scoped. vardeclarations are hoisted but initialized asundefined.varcan be re-declared within the same scope without an error.
Let me explain characteristics of var with a diagram –

What is let in JavaScript?
The let keyword was introduced in ES6 to address the issues of var by providing block-scoped variables.
Example of let
// LogicalDuniya - Example of let -
let y = 10;
console.log(y); // Output = 10
if (true) {
let y = 20;
console.log(y); // Output = 20
}
console.log(y); // Output = 10 (Original value retained)
Key Characteristics of let
letis block-scoped, meaning it only exists within the{}block it was declared in.letcannot be re-declared in the same scope.letis hoisted, but not initialized (using it before declaration causes an error).
var | let | const | |
| Scope | Function Scope | Block Scope | Block Scope |
| Hoisting | Hoisted as undefined | Hoisted but not initialized | Hoisted but not initialized |
| Re-declaration | Allowed | Not allowed | Not allowed |
| Mutability | Mutable | Mutable | Immutable |
Best Practices for Usage
- Use
varonly if supporting old JavaScript versions (not recommended). - Use
letwhen the variable value may change in the future. - Use
constwhen the variable should remain constant after assignment.
JavaScript let Vs var in Local Scope?
Local scope means that variables are accessible only within a specific function or block.
Example: Local Scope with var
// LogicalDuniya - Example of var -
function logicalFun() {
if (true) {
var a = 100;
}
console.log(a); // Output = 100 (Accessible outside the if block)
}
logicalFun();
Example: Local Scope with let
// LogicalDuniya - Example of let -
function logicalFoo() {
if (true) {
let b = 200;
}
console.log(b); // Output = ReferenceError: b is not defined
}
logicalFoo();
Key Takeaways
varleaks out of the block scope and it’s value is accessible outside.letis truly block-scoped, meaning it only exists inside the block where it was defined.
π Interview Tip #1
Interviewer : what would you be prefer between var and let?
You : I would always prefer to useletovervarwhen defining variables inside blocks to prevent unintended behavior due to scope leakage.
JavaScript let Vs var in Function?
When variables are declared inside a function, their behavior depends on whether they are declared with var or let.
Example: Function Scope with var
// LogicalDuniya - Example of let -
function logicalFunctionVar() {
var x = 10;
}
console.log(x); // Output = Uncaught ReferenceError: x is not defined
Example: Function Scope with let
// LogicalDuniya - Example of let -
function testFunctionLet() {
let y = 20;
}
console.log(y); // Output = Uncaught ReferenceError: y is not defined
Key Takeaways
- Both
varandletare not accessible outside the function where they are declared. - This is an example where
varandletbehave similarly.
JavaScript let Vs var in Class?
When declaring variables inside a class, let and var behave differently in terms of scoping.
Example: Using var in Class
// LogicalDuniya - Example of var -
class LogicalExampleClass {
constructor() {
var x = 30;
}
}
console.log(new LogicalExampleClass().x); // Output = Undefined
Example: Using let in Class
// LogicalDuniya - Example of let -
class Example {
constructor() {
let y = 40;
}
}
console.log(new Example().y); // Output = Undefined (let is block-scoped)
Key Takeaways
- Both var and let are not attached to the instance when declared inside a constructor.
- If you want a class property, define it using
this.variableName.
JavaScript let Vs var in Lambda Function?
Lambda functions (also called arrow functions) handle let and var in the same way as regular functions.
Example: Using var in Arrow Function
// LogicalDuniya - Example of var in Lambda / Arrow Function -
const LogicalVarLambda = () => {
var a = 50;
}
console.log(a); // Output = Uncaught ReferenceError: a is not defined
Example: Using let in Arrow Function
// LogicalDuniya - Example of let in Lambda / Arrow Function -
const testLetLambda = () => {
let b = 60;
}
console.log(b); // Output = Uncaught ReferenceError: a is not defined
Key Takeaways
- Variables declared with
varandletinside an arrow function do not exist outside of the function scope.
π Interview Tip #2
Interviewer : What would you prefer between var and let in Lambda Function / Arrow Function?
You : I would always prefer let to avoid potential re-declaration issues with var.
Final Notes
In this blog, we explored the difference between let and var in JavaScript. Hereβs what we learned:
varis function-scoped and allows re-declaration.letis block-scoped and does not allow re-declaration in the same scope.varvariables leak out of block scopes, whereasletvariables do not.- In functions, both
varandletare scoped to the function but behave differently within blocks. - In classes, neither
varnorletautomatically becomes a class property unless attached tothis. - In lambda functions,
varandletbehave the same as in regular functions.
Overall, Here is when to Use let and var –
var | let | const | |
| Scope | Function Scope | Block Scope | Block Scope |
| Hoisting | Hoisted as undefined | Hoisted but not initialized | Hoisted but not initialized |
| Re-declaration | Allowed | Not allowed | Not allowed |
| Mutability | Mutable | Mutable | Immutable |
By following these guidelines and understanding their differences, you can write better-structured, maintainable, and bug-free JavaScript code.
Next π
- To learn more simple and detailed JavaScript guides, do check our βFreeβ JavaScript Mastery Series.
- To use hundreds of βFreeβ tools to fasten your daily development, testing and content writing tasks, visit β Tools.LogicalDuniya.com
- To know about more JS Tutorials and other latest technologies & concepts, visit our Tutorials page.
Happy Learning,
Shubham : )