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/theQuandary May 19 '26

If you were told a simple find/replace could net you 3-8% performance increase across your entire codebase for free, would you do it? In basically any other case I think you would.

JS made a big mistake not using let block/expressions instead as they had all the upsides people wanted and none of the downsides while having the benefits of proposals like the do expression.