jamesallardice/jslint-error-explanations

Unexpected '++' in a for loop

filipjares opened this issue · 5 comments

Hello,

first of all I want to say that I am really grateful for both JSLint and JSLintErrors projects.

I have a question regarding this classical idiom from the C language:

for (i = 0; i < someArray.length; i++) {
    // do something
}

The JSLint gives me the following error and the reason is not clear to me.

Unexpected '++'.

Why does the JSLint tool discourage the usage of the ++ operator?

Thanks, I'm glad you like the project! The error you mention is probably the one that causes the most confusion, and the reason for it is simply the preference of Douglas Crockford, who wrote and maintains JSLint. Many people (including me) disagree with this choice, but fortunately you can work around it by setting the plusplus option to true in your config. If you have per-file configuration, just add a jslint directive to the top of the file:

/*jslint plusplus: true */

Hope that helps.

I see. Thank you for your explanation.

I would like to know if Douglas Crockford's reasons would convince me...

XP1 commented

@filipjares

Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?:
http://stackoverflow.com/questions/971312/why-avoid-increment-and-decrement-operators-in-javascript

Crockford on JavaScript - Section 8: Programming Style & Your Brain:
http://www.youtube.com/watch?v=taaEzHI9xyY&t=50m42s

@XP1 Thank you for the reference!

just do i += 1

like

var i;
for (i = 0; i < 6; i += 1) {
code block to be executed
}