The `var` statement declares variables that are function-scoped or globally-scoped. `var` declarations are hoisted, meaning declaring a variable anywhere in the code is equivalent to declaring it at the top of the function or the script.
Even if hoisted, it is still recommended to declare the variable inside the block it is used. This improves readability and maintainability because it makes it clear where the variable is being used. The code then becomes easier to understand and follow, especially for other developers who may be working on the same codebase.
When `var` declaration is used outside of a block, the declaration should be done at the uppermost level where it is used. When possible, use `let` or `const`, which allow for block-scoped variables.
\[~ann.campbell.2] WDYT if we change this rule to not function scope but to java-like scopes (function+loops+if)?
Following to this convention will ease transition to ES2015 variables declarations (let and const)
=== on 17 Feb 2016, 09:24:03 Elena Vilchik wrote:
\[~ann.campbell.2] I removed part about functions as it probably will produce FP. See
----
var y; // OK
function foo(p) {
if (y) {
bar(y);
}
y = p;
}
for (var j = 1; j < 10; j++) {
foo(j)
}
----
=== on 18 Feb 2016, 09:49:46 Elena Vilchik wrote:
\[~ann.campbell.2] Looks like after removing this "function" thing this rule has not much in common with RSPEC-1899. May be we should make it as separate RSPEC, WDYT? (cc [~pierre-yves.nicolas])