JSLint message: Unused variables

JavascriptJqueryJslint

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Solution 5 - Javascript

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.

Solution 6 - Javascript

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.

Solution 7 - Javascript

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('');

Solution 8 - Javascript

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, ...).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheFitGeekGirlView Question on Stackoverflow
Solution 1 - JavascriptnickfView Answer on Stackoverflow
Solution 2 - JavascriptPuZZleDucKView Answer on Stackoverflow
Solution 3 - JavascriptnickfView Answer on Stackoverflow
Solution 4 - JavascriptMagnus HoffView Answer on Stackoverflow
Solution 5 - Javascriptxn.View Answer on Stackoverflow
Solution 6 - JavascriptDharmaTurtleView Answer on Stackoverflow
Solution 7 - JavascriptGreg DomjanView Answer on Stackoverflow
Solution 8 - JavascriptPilipeView Answer on Stackoverflow