Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected an assignment or function call and instead saw an expression

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}; 
like image 697
ryanve Avatar asked Mar 02 '12 13:03

ryanve


People also ask

How do you expected an assignment or function call and instead saw an expression?

js error "Expected an assignment or function call and instead saw an expression" occurs when we forget to return a value from a function. To solve the error, make sure to explicitly use a return statement or implicitly return using an arrow function.

How do you call a function in react?

In React, the onClick handler allows you to call a function and perform an action when an element is clicked. onClick is the cornerstone of any React app. Click on any of the examples below to see code snippets and common uses: Call a Function After Clicking a Button.


2 Answers

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

like image 137
T.J. Crowder Avatar answered Oct 02 '22 00:10

T.J. Crowder


You can add the following line to ignore that warning:

/*jshint -W030 */

You can read more about it here.

like image 36
ReedD Avatar answered Oct 02 '22 02:10

ReedD