DEV Community

Discussion on: 3 reasons to use 'var' in JavaScript

 
thebearingedge profile image
Tim Davis • Edited

For mine, let and const are way more useful than var, so I avoid var and in my personal projects I have linting rules against it.

Same here. But I teach beginners and coding style is the least of their worries and they need to be prepared to slot into the team they're hired onto.

There are folks with other coding styles that rely on hoisting, redeclaration and leaky block scopes that benefit from var, so for them it makes sense to do the opposite and stick to var and maybe even ban let or const in their linting rules.

This is an interesting observation that I've seen as well. IME, they just write sloppy or confusing code in general. It's always possible to write code that avoids the pitfalls of var entirely as long as the style is reasonable. What's weird is that the style of my reference solutions function identically when you refactor them to the "right" way and use const / let. This is probably why it's so rare for my students get "tripped up" by the pitfalls of var. When they do, the code they're writing makes no sense anyway, which is expected of beginners.

One thing is clear, and is that is way more popular to prefer let/const.

Yes, you are absolutely right, which is why they're also equipped for that. The motivation behind my comments also boils down to your point. There are social pressures and media being produced that discourage thinking critically about how things work, and when they're appropriate. Often things get reduced to memes and jokes and if I "warn" them about anything, it's that they're going to encounter people, videos, and writings that are reductive and unproductive. They need to be careful not to trick themselves into thinking they're any better at building useful software by "agreeing" with anybody's lopsided opinions.