37
votes

I'm totally cool with this JSLint error. How can I tolerate it? Is there a flag or checkbox for it?

You get it when you do stuff like:

v && arr.push(v);

as opposed to:

if (v) {
    arr.push(v);
}

Both do the same exact thing. If you put:

window.test = function(v) {
    'use strict';
    var arr = [];
    if (v) {
        arr.push(v);
    }
    return arr;
};

into the minifier it minifies down to this anyway:

window.test=function(a){var b=[];a&&b.push(a);return b};
4

4 Answers

76
votes

I don't think JSLint has an option to turn that off.

JSHint (a fork with more options) has an option for it, though: The expr option, documented as "if ExpressionStatement should be allowed as Programs".

36
votes

You can add the following line to ignore that warning:

/*jshint -W030 */

You can read more about it here.

10
votes

People who are looking how to suppress it when using ESLint. You can ingnore it by writing the following comment just above the line no-unused-expressions

// eslint-disable-next-line no-unused-expressions

You can also suppress the warning for the entire file by placing the following comment at the very top of the file

/* eslint-disable no-unused-expressions */
9
votes

There's no option for this in JSLint. You can circumvent it using:

var dummy = v && arr.push(v);

NB: dummy evaluates to true after that.

Another workaround could be:

function expression(statement) { 
 'use strict';
 return statement; 
}
expression(v && arr.push);