49 lines
707 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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
----
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
----
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;
}
----