Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Error - "Expected an assignment or function call and instead saw an expression"?

I am using JSLint to ensure my JavaScript is "strict" and I'm getting the following error:

Expected an assignment or function call and instead saw an expression

On the following code:

(my_var > 0 ) ? $("#abc").html(my_array.join('')) : $("#abc").html('<h2>Hello ' + persons_name);

Any ideas why I'm getting such an error? Also, I'm using jQuery as seen in the above code, in case that makes a difference.

like image 517
HeatherK Avatar asked Nov 12 '10 16:11

HeatherK


1 Answers

My guess would be that JSLint is unhappy since you're using the ternary operator, and you're not doing anything with the value. Refactoring this into the equivalent:

if (my_var > 0 ) {
  $("#abc").html(my_array.join(''));
} else {
  $("#abc").html('<h2>Hello ' + persons_name);
}

would eliminate the error. If for some reason you're really attached to using the ternary operator, the "right" way to use it would be:

$("#abc").html((my_var > 0) ? my_array.join('') : '<h2>Hello ' + persons_name);
like image 146
Mark Bessey Avatar answered Oct 20 '22 01:10

Mark Bessey