r/learnjavascript • • May 18 '26

Do people still use var in JavaScript?

I usually see let and const everywhere, and I understand why they are preferred because they are block-scoped and safer.

But I also heard an opinion that var can still be useful when declaring variables inside a function, because it makes it clear that the variable belongs to the whole function scope and may be used across different blocks inside that function.

For example, let feels more natural for variables inside blocks like if, try/catch, loops, etc., while var could theoretically show that the variable is intended to be available throughout the function. Does anyone actually think about var this way, or is var basically avoided completely in modern JavaScript?

24 Upvotes

42 comments sorted by

View all comments

Show parent comment

1

u/ILMTitan May 20 '26

While the var keyword in Java is great, it doesn't change the semantics of a variable declaration. The let and const keywords do, and help avoid a host of bugs caused by the counter-intuitive semantics of the var keyword.