DEV Community

Discussion on: Where do I start? Part 3: JavaScript

Collapse
 
arximughal profile image
Muhammad Arslan Aslam

According to some JavaScript experts, yes. You shouldn't use var. Use const &/|| let.

The reason for that,

  • var isn't block-scoped
  • No warnings/errors given if you declare the same variable twice using var
  • Some modern linters give warnings stating var is a bad coding practice

More in-depth article here:

Collapse
 
bigmabe profile image
E.Mabry

Thanks for that explanation. I never looked at it that way Muhammad. After reading the article I understand. I just have never had any problem with it. I do see the benefit with block-scoped and I do recognize the errors it gives using some linters. I will give it a try.

Thread Thread
 
arximughal profile image
Muhammad Arslan Aslam

Glad I could help <3

Collapse
 
zac_heisey profile image
Zac Heisey • Edited

I think Muhammad brings up some solid points here as to why you wouldn't want to use var. However, I don't agree with the author's notion that var should never be used. In fact, I'd argue that using var in the early stages of learning JavaScript will make the concept of declaring, changing, and working with variables easier to understand.

Chris Ferdinandi, a vanilla JavaScipt expert, has a nice explanation on why he doesn't use let or const in his projects: gomakethings.com/why-i-dont-use-le...

His reasoning: let and const aren't as backwards compatible as var, they can't be polyfilled, and trying to determing which type to use adds unnecessary cognitive overhead (especially for beginners).

Thread Thread
 
arximughal profile image
Muhammad Arslan Aslam

I totally agree with you. For a beginner, using var can be a good thing to understand working with variables.