Expected an assignment or function call and instead saw an expression

60,448

Solution 1

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".

Solution 2

You can add the following line to ignore that warning:

/*jshint -W030 */

You can read more about it here.

Solution 3

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 */

Solution 4

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);
Share:
60,448
ryanve
Author by

ryanve

graffing webmural.com and s9a.page

Updated on April 09, 2020

Comments

  • ryanve
    ryanve about 4 years

    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};
    
  • T.J. Crowder
    T.J. Crowder about 12 years
    @diEcho: You can also download and use it locally (that's what I do). I run it via NodeJS with my preferred options set, dramatically helps my confidence in correct code. :-)
  • ryanve
    ryanve about 12 years
    It'll work but it's silly. The minifier will actually sort that out and but for readability it'd be better to just use the if statement than that IMO.
  • KooiInc
    KooiInc about 12 years
    That's true. But you asked for a way to tolerate is. Added another workaround. It's like Crockford said somewhere: jsLint is designed to be a pain in the ass for programmers ;~)
  • Arseni Mourzenko
    Arseni Mourzenko over 9 years
    The answer is completely unrelated to the question, which was about JSLint, not JSHint.
  • Charlie Martin
    Charlie Martin about 9 years
    Yet its still the most useful answer for the majority of people who land here.
  • iconoclast
    iconoclast over 8 years
    "completely unrelated"? Because JSHint is completely unrelated to JSLint?
  • faebster
    faebster about 8 years
    WRONG the wonderful && hack does not work in jsLint... maybe depends on version...
  • Mohammad Kermani
    Mohammad Kermani over 7 years
    Can describe more please, why that warning happened?
  • johannchopin
    johannchopin about 4 years
    I recommend you to also provide the link of the documentation of this specific rule no-unused-expressions