DEV Community

Should You Truly Never Use var?

John Wolfe on October 21, 2017

Some controversy in the Javascript community has emerged over this classic variable declaration. So, are the E6 purists correct and we should forev...
Collapse
 
nektro profile image
Meghan (she/her)

For me, nope never again using var. it’s inefficient, hard to maintain, and error prone.

I don’t how unpopular this opinion is, but I think let and const should have been the only ways from the beginning. I know this isn’t the case because JavaScript was designed in about a week, but If we ever have a “JavaScript 2” then I would propose var being removed.

Collapse
 
johnwolfe820 profile image
John Wolfe

I think most people agree with you. Kyle Simpson's point about using it in top level variables that are shared across multiple scopes I think is legitimate, but even in that context its only advantage is to make the code more readable. It seems to me that selectively trying to use it in those situations would be error prone, like you mentioned.

I'm wondering if anyone has legitimate use cases where var remains the only viable variable declaration.

Collapse
 
nesvand profile image
Andrew Nesvadba

I don’t think there’s going to be a case where var is the “only viable” - all we’re left with is considering code style conventions like the one Simpson has suggested.

Will I adopt it personally? Probably not but it’s an interesting idea all the same.

Collapse
 
donut87 profile image
Christian Baer

If you have a variable in top-level shared across multiple scopes, then you have other things to worry about. Constants are fine btw. but variables? Things that can change? Over multiple scopes? Didn't we already discover this as a code smell?

Collapse
 
courier10pt profile image
Bob van Hoove

var - being function scoped - led to the 'habit' of declaring all variables at the top of the function. As long as you stuck to that, it would make bloated functions really stand out.

Lucky thing is you can use the same approach with let and it just works. I just hope the variables first approach will stay.

Thanks for writing the article.

Collapse
 
paulsmithkc profile image
Paul Smith

The "variables first approach" is an anti-pattern that originated in Fortran and continued with C, because compilers the time were not very smart and did not do a good job of optimizing code.

Putting all your variables at the top forces the compiler to allocate space for all of them on the stack from the beginning, even if you don't end up using them. It also prevents the compiler from intelligently warning you about uninitialized variables. That second point may seem small, but can be a deadly problem in medical software.

If you switch to use const to declare most of your variables when you need them there are several benefits:

  • Faster Code, because the compiler tends to put variables into registers instead of the stack
  • Less stack swapping because the compiler knows what variables are "live" and which ones have fallen out of scope
  • Better Readability, because your reader isn't hit with a wall of variables which may or may not be used later, but have an initial state which must be tracked and an unknown purpose.
  • By declaring the variables when you can actually initialize them and use them, the reader has a clear understanding of the purpose of the variables and doesn't have to track 10 variables that you might use later.
  • You don't accidentally use a variable that either hasn't been initialized or has been initialized to a non-sensical value. (Initializing to zero isn't always safe. A lot of people have died from programs where variables where erroneously initialized to zero.)
Collapse
 
alexdevmotion profile image
Alexandru Constantin • Edited

After reading your argument, I'm still sticking to my gut feeling of never using var

Collapse
 
denishowe profile image
Denis Howe

Does any language apart from JavaScript distinguish between function scope and other block scopes? Why should JavaScript retain a special keyword to make that distinction (other than as a memorial to historical implementation details)? Someone who is incapable of working out the scope of a let variable will probably not understand var either.

Collapse
 
sammyisa profile image
Sammy Israwi

I've tried to avoid using var ever since I learned about let and const. But since I watched Kyle Simpson's course on Frontend Masters (same argument as his article that you mentioned) I've been rethinking it.

Lately I've been using/experimenting again var unless I need to take advantage of block scoping.

Collapse
 
antonfrattaroli profile image
Anton Frattaroli

I started using let, stopped due to users experiencing issues on their iPads. Still too new. Certainly not introducing babel in an application for some nearly trivial differences in how code runs.

Collapse
 
sakex profile image
sakex

That’s why you should use Babel

Collapse
 
reegodev profile image
Matteo Rigon

If you wanna use es6 in your production code the only real choice is to use a compiler.
If you don't think setting up a compiler is worth then you don't really need es6

Collapse
 
johnwolfe820 profile image
John Wolfe

that's interesting, so you still have to use var?

Collapse
 
antonfrattaroli profile image
Anton Frattaroli • Edited

Yeah, not that I consider it a hardship. Safari on iOS <= 9.3 (2015-2016) doesn't support let.