If you use var the variable is declared within the scope you are in (e.g. of the function). If you don’t use var , the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches.
With var it’s still possible as JS when it compiles all scripts it stack declaration of all variables to the very top of the global script, and only assigns value when script triggers the block of code where variable is declared.
Yes, if you decided to don’t support older browser or if you are using a transpiler. In ES6, there’s no any reason to prefer var in place of let or const . The only useful application of var is that a global can be redefined in global scope multiple times without causing an error.
When you use var , you are instantiating a variable in the current scope. This will also prevent access of variables named the same in higher scope, within the current scope.
Is it better to use let or VAR?
let is preferable to var because it reduces the scope in which an identifier is visible. It allows us to safely declare variables at the site of first use.
Is var a bad practice?
var speeds up the writing, but may slow down the reading a bit. It’s obviously not a code behaviour rule like “Always initialize variables” because the two alternatives (writing var and writing the type) have exactly the same behaviour. So it’s not a critical rule.