JSLint message: Unused variables

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.


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

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.


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.


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.


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.