Some controversy in the Javascript community has emerged over this classic variable declaration. So, are the E6 purists correct and we should forev...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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.
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.
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?
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.
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:
After reading your argument, I'm still sticking to my gut feeling of never using
var
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.
I've tried to avoid using
var
ever since I learned aboutlet
andconst
. 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.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.
That’s why you should use Babel
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
that's interesting, so you still have to use var?
Yeah, not that I consider it a hardship. Safari on iOS <= 9.3 (2015-2016) doesn't support let.