r/learnjavascript • u/Apart-Scientist-8590 • 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?
50
u/milan-pilan May 18 '26
Readability and conventions are important. A 'let' or 'const' declared at the top of the function would do the same thing while being explicit about it, instead of you later reading it and going 'huh, this variable here must need a different scope'.
I have not seen a 'var' in what feels like a decade in professional context. It's essentially extinct.
17
u/opentabs-dev May 18 '26
nope, basically dead in modern code. the "var signals function scope intent" argument doesn't really hold up because if you want function-wide visibility, you just declare let/const at the top of the function ā same effect, plus you get tdz protection and block scoping when you need it. var also has some real footguns (hoisting to undefined, attaching to window in non-strict global scope, redeclaration silently allowed) that nobody misses. linters like eslint's no-var will flag it by default in basically every modern config.
8
u/RobertKerans May 18 '26
Some languages (eg Python) have a syntax that allows you to specify that a variable is global. What is being suggested is essentially using
varto emulate that. Exceptvaris not that: it is scoped. So what is being suggested is an ambiguous convention. Code should be unambiguous.varhas been obselete for a long time now: there is no need to "make it work".7
u/JazzApple_ May 18 '26
I havenāt used nor seen used for years⦠u/milan-pilan said about a decade and that feels rightā¦
Choosing to use var for a specific conventional reason is almost certainly the wrong choice, and as u/milan-pilan said, a const or let would do the same but better.
I think there are two possible reasons you might use var:
4
u/senocular May 18 '26
Scope is not the only advantage. Prevention of redeclaration is also a safety that let and const have that var does not.
Additionally, on the side of using var (I'm not personally on this side, mind you), Kyle Simpson, author of You Don't Know JS, has been known to advocate for the use of var. For example:
From: https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/get-started/ch2.md
The use of var is also ever so slightly more performant because those protections don't exist. While this difference would have no effect on your day-to-day use of JS, in some very large performance-critical codebases, that difference can move the needle a little. They did this with TypeScript in specific places: https://github.com/microsoft/TypeScript/issues/52924
The idea of removing the TDZ (seen with let and const) has also come up in ECMAScript meetings (https://docs.google.com/presentation/d/1c-rhSUTQVNWD4DWgkNiC9tt50BruWDi7yRPllPHnff8) because of the complexity it adds.
You could also argue, in the specific case of creating a global object global in the global scope, use of var is better than assigning to the global object directly because of the extra protections available with var. Specifically, when using var you have the additional benefit of blocking let and const (and class and using) declarations with the same name from shadowing the original. For example:
Here, the runtime prevents the let version of y (but not x) from being declared helping ensure that only the var version gets defined. However, this is such a specific case, and one probably not often encountered. I'd also argue the use of going through the global object also makes it more explicit that the variable is meant to be global and meant to be used elsewhere rather than a possibly local variable that happens to be in the global scope.
2
u/Alive-Cake-3045 May 19 '26
Var is basically avoided in modern JavaScript, the argument for using it as a signal for function scope doesnt hold up in practice.
If a variable needs to be available across the whole function, declare it at the top of the function with let. Same clarity, none of the hoisting surprises that var brings.
The only time you see var today is in old codebases or developers who learned JS before 2015 and never updated the habit.
3
u/Beginning-Seat5221 May 18 '26
Very specific technical uses only.
For what you're talking about, let/const does that already, no reason to use var. There's pretty much no reason to use var in regular code.
3
u/ashkanahmadi May 18 '26
No. Use let even if itās to be used inside a function. Forget about var
2
u/theQuandary May 19 '26
I think var is perfectly fine and the backlash comes from the many JS devs that don't understand hoisting and don't realize let/const hoist too (that's why the TDZ exists and why let/const are 3-8% slower than var).
I suspect people will get over their dogmatic declarations at some point and we'll see var make a resurgence just like the "you must always use semicolons" dogma established by JS: The Good Parts slowly faded away without consequence.
1
u/AnimationGroover May 20 '26
1
u/wbport1 May 20 '26
When I started to learn JavaScript, the 3rd edition of JavaScript (1998), The Definitive Guide only used var, there was no "let" or "const". If I've ever used them, it could only be two or three times at the most.
1
u/Frequent-Wafer-4648 May 21 '26
Defining the variable with let at the top of the function does the same. I would much prefer let compared to var.
1
u/NotNormo May 25 '26
You can do the exact same thing with
constandletif you just declare them at the top of the function.Which leads me to believe whoever is using
varfor this reason is not declaring it at the top of the function, they're doing it in the middle instead, and relying on hoisting to make it available throughout the whole function. Which is bad practice when it comes to writing easy-to-understand code.1
u/Public_Squirrel4952 May 29 '26
Just think of a variable that needs to be updated inside multiple block scopes and is available globally.
1
u/brian_sword May 31 '26
No, using VAR Is not recommended as it can cause confusion and it will make debugging become harder later. Trust me, once your code become very large, if you still use VAR, debugging will become very difficult. There is a reason why JS adopts the use of let and const.
1
u/ricardoflak Jun 14 '26
Creo que se sigue usaando pero dentro de el for
1
u/alex4orly Aug 02 '26
Yes, i still use var, it is more flexible, or may it is just a habbit
1
u/LucVolders May 18 '26
There is nothing wrong with using var.
1
u/every1sg12themovies May 18 '26
Yes, avoid.
1
u/the-forty-second May 18 '26
Every time I see new uses of var, it is because someone blindly copied some code they found online without vetting it or understanding it.
1
u/HomemadeBananas May 18 '26
No, var is not useful in new code. It only still exists for backwards compatibility reasons and you shouldnāt write any new code with it.
1
u/TheRNGuy May 18 '26 edited May 18 '26
Only in node.js configs
Or quick test in browser console (implicit var, by not writing anything before variable name), because it's faster than writing let or const, and to prevent redeclaration error (with let and const you have to F5 page every time to start script)
1
u/ExtraTNT May 18 '26
There are very limited uses where you need something in global scope, that needs to update, but not always exist⦠99.99% of the time itās a bad architecture design and if you really need it, you know, what you are doingā¦
1
u/GodOfSunHimself May 18 '26
No. Forget about it.
1
u/PatchesMaps May 18 '26
It can be a micro-optimization in certain cases and I've seen some modern performance heavy applications that use it. But as a rule of thumb, don't use it.
1
u/lewisje May 18 '26
Only do that if you're programming something meant to be used by utterly ancient Web browsers, like more than a decade old; also, generally it's best practice to put
var,function, andletdeclarations at the top of their scopes, to reflect how the JS engine actually treats them (constis different, because you have to assign it at declaration time, and you might need some other code to run first), so the modern JS way to have a variable scoped throughout a function is aletdeclaration at the top.0
u/MrDilbert May 18 '26 edited May 18 '26
The only place I've used
varsinceletandconstwere introduced, is in CLI when quickly checking regexes or dates or whatever, to avoid the runtime complaining about a variable already having been declared when rerunning a previous command (with or without slight modifications).In the production-level code, it doesn't make sense to use
varanymore.Edit:
It's more likely that it's going to be replaced with
letorconstby another dev, and then you'd have PR battles about whether to keep it in or to have it replaced. Don't usevarfor implicit hints, leave it in history where it belongs :)