var – How to use the var keyword in JavaScript
Still using var in JavaScript? You might be setting silent traps in your code. Discover why modern developers are ditching var for safer, smarter alternatives like let and const.
nanadwumor

-
var is function-scoped, not block-scoped — it leaks outside {} blocks.
-
var allows redeclaration and is hoisted with value undefined, which can cause bugs.
-
let is block-scoped and avoids var’s quirks — safer for loops and blocks.
-
🔒 Memory Tip: let is like a net — once inside, it’s not let out.
RECOMMENDED ARTICLES
let – How to Use the ‘let’ Keyword in JavaScript
Still using [crayon-6895b2f1df53b130568123-i/] in 2025? It’s time to level up. Discover why [crayon-6895b2f1df54b157987082-i/] is the smarter, safer way to declare variables in modern JavaScript—and how it can save your code from hidden bugs. ...
What is the difference between strict and non-strict modes in Javascript?
If you ask your code to be executed under strict mode, JavaScript will force you to declare your variables else it will throw an exception or call foul that your code has error. Javascript can be executed in a strict mode just as a language like...
1 |
var name = "Alice"; |
It still works, but it’s mostly replaced by let and const in modern JavaScript because of some quirky behavior.
Key Features of var
-
Function-Scoped
1 2 3 4 5 6 |
function test() { if (true) { var x = 10; } console.log(x); // 10 (not block-scoped) } |
Output
10
🧠 Memory Aid: You can remember this as the fact that functions are made up of variables, so var is function-scoped.
Why var is Risky in Modern Code
Issue | Explanation |
Not block-scoped | Variables can “leak” outside blocks |
Redeclaration allowed |
Easy to accidentally overwrite |
Hoisted with undefined |
Can lead to confusing bugs |
For example,
1 2 3 4 |
for (var i = 0; i < 3; i++) { console.log("Inside loop:", i); } console.log("After loop ends, i is:", i); |
Output
Inside loop: 0
Inside loop: 1
Inside loop: 2
After loop ends, i is: 3
Explanation
1 2 3 4 |
for (var i = 0; i < 3; i++) { console.log("Inside loop:", i); } console.log("After loop ends, i is:", i); |
-
var is function-scoped, not block-scoped.
-
So i is declared in the surrounding function or global scope, not limited to the for loop block.
-
After the loop completes, i has been incremented to 3, and since it’s still in scope, it can be accessed outside the loop.
- var is not block-scoped, so the i variable lives outside the loop too.
- After the loop finishes, i still exists and its final value is 3.
Better with 'let'
1 2 3 4 |
for (let i = 0; i < 3; i++) { console.log("Inside loop:", i); } console.log("After loop ends, i is:", i); // ❌ ReferenceError |
Output
Inside loop: 0
Inside loop: 1
Inside loop: 2
ReferenceError: i is not defined
Explanation
-
let is block-scoped — it lives only inside the for loop block.
-
After the loop ends, i is no longer accessible, so accessing it outside the block throws a ReferenceError.
🔒
Memory Aid: Think of
let as a net. Once a variable is caught inside a block, it’s not let out.
So
let is block-scoped and disappears outside the block.
You May Also Like…
let – How to Use the ‘let’ Keyword in JavaScript
Still using [crayon-6895b2f1df53b130568123-i/] in 2025? It’s time to level up. Discover why...
What is the difference between strict and non-strict modes in Javascript?
If you ask your code to be executed under strict mode, JavaScript will force you to declare your...
0 Comments