With the new keyword let
for declaration of variables in javascript ES6, I can no longer think of good reasons to use var
. So far, I have been doing just that and I do not see any disadvantage of using let
ALL THE TIME.
What are good reasons to use var
today? Is it a good practice to use let
all the time today?
let allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped. Variable declared by let cannot be redeclared and must be declared before use whereas variables declared with var keyword are hoisted.
For you question directly, var is probably still used for legacy reasons. It is supported in all versions of JavaScript and it would be a bother to change every example on the internet. The only real advantage to var is it's compatibility. If you are writing for old platforms like .
Always declare JavaScript variables with var , let , or const . The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browsers, you must use var .
IMO there is no definite advantage of using var
over let
, other than for its scope. One reason might be to support older browsers perhaps (if you don't plan to use an ES6 to ES5 compiler like Babel).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With