ECMAScript 6 introduced let
and const
keywords for variable declaration.
That being said, is there any reason to use var
to declare a variable?
ECMAScript 6 introduced let
and const
keywords for variable declaration.
That being said, is there any reason to use var
to declare a variable?
Backwards compatibility (writing/maintaining ES5) or the ability to write/maintain sloppy code.
var
, unlike let
or const
can redefine a variable multiple times in the same scope. Particularly if you are refactoring something that was already a mess, you might need to stick with var
for a little while.
There is plenty of code out there that redefines i
in multiple for
loops, which would throw an error if you replace one instance with let
.
This is something that linting tools have been able to catch for years, but that doesn't mean all the sketchy code out there went away.
The main reason is backwards compatibility. Other than that, not that I can see. So if you are using a transpiler then really no point in using var. Unless you need a global variable but that is evil.