79

what can I do if JSLint complains about "i" being an unused variable in such a scenario:

var items = "<option selected></option>";
$.each(data, function (i, item) {
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

(i, item) is the required order of parameters and I'm only using "item".

Is there any other solution than tolerating unused variables or rewriting the $.each to use the index, both solutions which I would prefer not to do?

Thanks in advance.

Update: I appreciate all the suggestions but this code is simply an example to show you what I mean and I'm interested to see a general solution, if there's any. Thanks.

TheFitGeekGirl
  • 1,233
  • 1
  • 12
  • 17
  • 6
    In this particular case `this` = `item` (http://api.jquery.com/jQuery.each/), so you wouldn't have to use either parameter. But this question should probably be answered in a more general sense. – Greg Jul 05 '11 at 13:48
  • It would be so nice if we could just do `$.each(data, function (, item)` – oscaralexander Feb 04 '14 at 14:14
  • 1
    [Many people use `_` for an unused parameter](http://stackoverflow.com/questions/9888725/skipping-parameters-in-callback-function), but I [see no way to tell JSLint](http://jslint.com/lint.html) to specifically ignore `_`, although that would be really nice. – David J. Sep 10 '14 at 02:19

8 Answers8

79

Try:

var items = "<option selected></option>";
/*jslint unparam: true*/
$.each(data, function (i, item) {
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});
/*jslint unparam: false*/  // so that you still get warnings from other functions
user2847749
  • 339
  • 2
  • 9
  • 27
nickf
  • 537,072
  • 198
  • 649
  • 721
25

I think this must be new in: http://www.jslint.com/help.html

"JSLint introduces a new reserved word: ignore"

So the above simply becomes:

$.each(data, function (ignore, item) {

i => ignore ... too easy. The rest of the code can remain the same, browsers are happy and JSLint is happy


Earlier (wrong) answer:

To placate both JsLint and browsers I needed to use:

function (d, i) {
        if (undefined !== win.undefined) {
            undefined(d);
        }
        return (i);
}

The browser crashed on "undefined(d)" due to undefined not being a function. So the "undefined !== win.undefined" skips the line if we are in a browser.

PuZZleDucK
  • 361
  • 3
  • 4
  • 14
    This new `ignore` way of suppressing warnings breaks down if the same function has more than one unused parameters. – oyenamit Aug 15 '15 at 16:33
  • you can use (parameter, ignore, ignore1, ignore2, value, ignore3), at least on Webstorm for Mac is working ad suppressing warnings – Paul N Oct 10 '17 at 17:32
1

How about using void to make explicit that you are intentionally not using the variable?

$.each(data, function (i, item, any, other, unused, vars) {
  void(i, any, other, unused, vars);
  items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

This is also useful in abstract functions that are expected to be overwritten, but where you want to show the signature, or in mocks, where you are ignoring arguments, but want to match the mocked function's signature.

xn.
  • 15,776
  • 2
  • 30
  • 34
1

I rename "i" as "unused". It still leaves the error obviously, but I see it in the list and know I've "checked" that error and am okay with it.

DharmaTurtle
  • 6,858
  • 6
  • 38
  • 52
  • This is not a bad idea, but if you do continuous integration and want to lint all code before allowing code to be merged then this doesn't work if you're treating warnings as errors. –  Jul 31 '15 at 03:28
  • 1
    As mentioned in [this](https://stackoverflow.com/a/31295922/185053) answer, rename the unused variable to "ignore" and it will be validated by JSLint. So "ignore" instead of "unused", and you're all set (limitation: supports only one unused variable in the same function) – Emilien Jul 23 '17 at 12:51
1

you could do this:

var items = "<option selected></option>";
$.each(data, function () {
    var item = arguments[1];
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

...but that's probably worse if you ask me.

nickf
  • 537,072
  • 198
  • 649
  • 721
  • 1
    @Schroedinger -- oh yeah, for sure. It's always a trade-off with JSLint. You just have to decide for yourself. – nickf Jul 05 '11 at 13:54
  • 8
    Fixes one warning, causes another "JS Lint: Use a named parameter" :) –  Oct 26 '12 at 18:59
1

A possible way to get rid of the warning in a way that is fairly self-documenting is to cause the unused variable to get used, like this:

// Utility function in project scope:
function unusedVariables(/* Put all your deliberately unused variables here */) {
    // pass
}

// And then, later:
var items = "<option selected></option>";
$.each(data, function (i, item) {
    unusedVariables(i); //< This is the new and magical line
    items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});

Of course, now you can get into the situation where you mark a variable as unused, and you still use it somewhere. Also, this method might be too verbose, depending on the context.

This method has the advantage that it is precise. Using /*jslint unparam*/ might be too broad.

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
  • 4
    This may fail JSLint's "empty block" test. – jokeyrhyme Aug 04 '13 at 04:52
  • 4
    This answer does 'work', though I would recommend against using it. I file it under "the solution is worse than the problem." – David J. Dec 07 '13 at 21:51
  • 1
    It'd be more helpful for other readers if you guys added some details about what is bad about this solution, @superluminary and DavidJames. I don't have a problem with people disliking this solution, but adding a comment saying just that isn't helping anybody. :) – Magnus Hoff Mar 03 '14 at 21:24
  • 6
    Apologies Magnus, comment deleted. The reason I dislike this solution is because you're adding non-semantic code merely for the purpose of fooling a specific version of a specific validator into passing your code. It's a hack, it doesn't add meaning to the code, and it won't age well. The proper solution is to modify JSLint. – superluminary Mar 03 '14 at 21:58
  • @superluminary Thank you for elaborating :) – Magnus Hoff Mar 03 '14 at 21:59
  • No problem @Magnus, and no offense meant. – superluminary Mar 03 '14 at 22:00
0

In this particular case of transforming an array/object http://api.jquery.com/jquery.map/ (or http://api.jquery.com/map/ ?) is an option.

var items = "<option selected></option>" + $.map(data, function (item) { 
    return "<option value='" + item.Value + "'>" + item.Text + "</option>";
}).get().join('');
Greg Domjan
  • 13,943
  • 6
  • 43
  • 59
0

If function has more than one unused parameters, you can use "ignore" like this :

function (ignoreFoo, ignoreBar, baz) {
}

It must simply begin with the reserved word "ignore" (ignore, ignoreFoo, ignoreBar, ...).

Pilipe
  • 134
  • 1
  • 8
  • This is substantially the same as [PuZZleDucK's answer](http://stackoverflow.com/a/31295922/3982001). If you add an answer when there are already some, and especially if they are much older and already upvoted, please make sure you are not just repeating another answer, or, if there's a difference, please explicitly cite the other one and point out the difference clearly. Thank you! – Fabio says Reinstate Monica Apr 25 '17 at 21:54
  • 2
    With the following code (3 unused variables) only the first "ignore" passes JSLint validation, the last two parameters trigger "Unused 'ignoreOne'" and "Unused 'ignoreTwo'" `videos.forEach(function (ignore, i, ignoreOne, ignoreTwo)` – Emilien Jul 23 '17 at 12:56
  • 'ignoreInterval' is declared but its value is never read. Now i get a longer error :( – noob7 Feb 25 '19 at 17:25