Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
154
rated 0 times [  156] [ 2]  / answers: 1 / hits: 21711  / 10 Years ago, sun, april 20, 2014, 12:00:00

I am using JSHint 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:



      var str = 'A=B|C=D'
var data = {};
var strArr = str.split( '|' );
for (var i = 0; i < strArr.length; i++) {
var a = strArr[i].split('=');
a[1] && (data[a[0].toLowerCase()] = a[1]); // Warning from JSHint
}


Any ideas why I'm getting such an error or how I can code to remove the error.


More From » arrays

 Answers
12

Here is a simplified version that gives the same warning:



var a, b;
a && (b = a);



Expected an assignment or function call and instead saw an expression




This means that you have an expression but do not assign the result to any variable. jshint doesn't care about what the actual expression is or that there are side effects. Even though you assign something inside of the expression, you are still ignoring the result of the expression.



There is another error by jslint if you care about it:




Unexpected assignment expression




This warns you that you may want to use == instead of = inside logical expressions. It's a common error, therefore you are discouraged to use assignments in logical expressions (even though it is exactly what you want here).



Basically, jshint/jslint do not like misuse of shortcut evaluation of logical operator as replacement for if statements. It assumes that if the result of an expression is not used, it probably shouldn't be an expression.


[#71379] Friday, April 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinarnoldp

Total Points: 10
Total Questions: 122
Total Answers: 109

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;