Suppose I have a function like this:
const hasAny = xs => {
for (const x of xs) {
return true;
}
return false;
};
eslint will complain that x
is not used, and indeed it isn't.
Can I write a for-of loop that does not declare any variables?
Without using a loop, we could have achieved that same output by using the following code. Without the loop in place, the code block is repetitive and consists of more lines. If we needed to increment through more numbers we would have needed to write even more lines of code. Let’s go over each expression in the loop to understand them fully.
the loop variable really has no meaning. Rewriting this code only to more readable. you've hidden that meaning by giving it the meaningless name "i". If [...] introducing bugs. For example, if you use "continue" anywhere in the loop, you will do one retry too much. Jan 10 '08 # 13 locally-translated version of a text string.
Without the loop in place, the code block is repetitive and consists of more lines. If we needed to increment through more numbers we would have needed to write even more lines of code. Let’s go over each expression in the loop to understand them fully. Our first expression is the initialization. This is what it looks like.
However, you can only use the numbers table in cases where you need to loop over data. If you want to loop over database objects and use dynamic SQL like in the previous paragraph, you'll need to resort to a cursor or WHILE loop (or the system stored procs). Luckily in those cases the data sets are small and there's no real performance impact.
According to ESLint issue #2173, you can set a comment to ignore the unused variable. For that, use the following code:
var some_unused_variable; // eslint-disable-line no-unused-vars
A community member of ESLint also states:
We now have a
varsIgnorePattern
option available for this use case (best suited for ignoring specific unused variable names or patterns across all your files).
And documentation states:
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names contain ignored or Ignored.
Examples of correct code for the { "varsIgnorePattern": "[iI]gnored" } option:
/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/ var firstVarIgnored = 1; var secondVar = 2; console.log(secondVar);
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