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.
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).
Kevin,
Great post, but why do you feel using "var" is a non-starter? Since, I use it quite often. Is this a bad coding practice?
According to some JavaScript experts, yes. You shouldn't use
var
. Useconst
&/||let
.The reason for that,
var
isn't block-scopedvar
var
is a bad coding practiceMore in-depth article here:
Should You Truly Never Use var?
John Wolfe
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.
Glad I could help <3
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 thatvar
should never be used. In fact, I'd argue that usingvar
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
orconst
in his projects: gomakethings.com/why-i-dont-use-le...His reasoning:
let
andconst
aren't as backwards compatible asvar
, they can't be polyfilled, and trying to determing which type to use adds unnecessary cognitive overhead (especially for beginners).I totally agree with you. For a beginner, using
var
can be a good thing to understand working with variables.