admin管理员组

文章数量:1241085

I am well aware of similar questions, but I still can't solve mine basing on those.

So, my code looks like this:

for (var e, i = 0; e = formErrors[i]; i += 1)

JSHint returns said error in char 39, so with ; after formErrors[i]. What can I do?

I am well aware of similar questions, but I still can't solve mine basing on those.

So, my code looks like this:

for (var e, i = 0; e = formErrors[i]; i += 1)

JSHint returns said error in char 39, so with ; after formErrors[i]. What can I do?

Share Improve this question edited Oct 22, 2015 at 8:44 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Oct 22, 2015 at 7:27 Tomek BuszewskiTomek Buszewski 7,95515 gold badges69 silver badges115 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 10

JSHint is warning you of a potential bug. It is expected that the 2nd part of a for statement will be a Boolean expression. Normally, you'd use one of the parison operators for this (==, ===, !=, > etc..). Since the expression is e = formErrors[i] it looks like it might be a mistake, possibly due to a missing equal sign. This is a mon typo that causes a lot of bugs.

Apparently, in this case it is not a bug, but rather an intentional usage of the fact that any expression evaluates to something, and an assignment expression evaluates to the assigned value:

var x;

alert(x = 1);

So the for statement actually assigns a new value to e, but also evaluates that value as a condition, casting it to Boolean if required.

You could refactor your code such that it both assigns the value and uses a casting operation that would satisfy JSHint and make the code a bit more obvious to the reader:

for (var e, i = 0; !!(e = formErrors[i]); i += 1)

The 2nd ! (the one directly in front of (e...) causes a casting to Boolean, but also negates it, the 1st ! reverts this negation.

That's just a very weird way of writing a loop. JsHint expects a boolean expression ("conditional") there, and judges that your assignment is a mistake and you actually wanted a parison (== instead of =).

What you should do is switch to the following mon array iteration idiom:

for (var i = 0; i < formErrors.length; i += 1) {
    var e = formErrors[i];
    …

(Which works the same as your original code for non-sparse formErrors arrays that contain no falsy values.)

Or alternatively, if you want to write non-idiomatic code, dump jshint :-)

e = formErrors[i] is an assignment, don't you want a condition there? use a ma after i=0 in that case, else put a condition after the semicolon.

Typically, the middle element in a for loop is the condition that is used to decide whether to continue the loop or not. You have an assignment there, if you wanted a condition you should use e === formErrors[i] (or with a double =, but that is usually not remended).

An assignment can technically work, because e can be, for example, some object (true) or null (false). But that is considered bad coding style, usually, and won't make for very readable code.

本文标签: javascriptJSHint Expected a conditional expression and instead saw an assignmentStack Overflow