== Why is this an issue? Variables that are declared inside a block but used outside of it (which is possible with a ``++var++``-style declaration) should be declared outside the block. === Noncompliant code example [source,javascript] ---- function doSomething(a, b) { if (a > b) { var x = a - b; // Noncompliant } if (a > 4) { console.log(x); } for (var i = 0; i < m; i++) { // Noncompliant, both loops use same variable } for (var i = 0; i < n; i++) { } return a + b; } ---- === Compliant solution [source,javascript] ---- function doSomething(a, b) { var x; if (a > b) { x = a - b; } if (a > 4) { console.log(x); } for (let i = 0; i < m; i++) { } for (let i = 0; i < n; i++) { } return a + b; } ---- ifdef::env-github,rspecator-view[] ''' == Implementation Specification (visible only on this page) === Message Consider moving declaration of 'XXX' as it is referenced outside current binding context. === Highlighting * primary: the variable definition * secondary: every reference to the variable outside of the current binding context. ** message: 'Outside reference.' ''' == Comments And Links (visible only on this page) === on 26 Jan 2016, 10:51:24 Elena Vilchik wrote: \[~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]) endif::env-github,rspecator-view[]