Select Page

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

August 05, 2025

var - how to use the var keyword in JavaScript


  • 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
let – How to Use the ‘let’ Keyword in JavaScript

Still using [crayon-6895895b7aff7077724704-i/] in 2025? It’s time to level up. Discover why [crayon-6895895b7b005711509663-i/] is the smarter, safer way to declare variables in modern JavaScript—and how it can save your code from hidden bugs. ...


var is the old way of declaring variables in JavaScript, available since the beginning of the language. For example,

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

A variable declared with var is only limited to the function it’s in,  not to a block {}. For example,

Output

10

var is function-scoped, not block-scoped. Even though x is declared inside the if block, it is hoisted to the top of the function scope. So when console.log(x) runs, x exists and has the value 10. If you use var outside a function, it becomes a global variable.

🧠 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, 

Output

Inside loop: 0
Inside loop: 1
Inside loop: 2
After loop ends, i is: 3

Explanation

  • 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'

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…




0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *