jamesallardice / jslint-error-explanations

:memo: Explanations of all the (sometimes cryptic) error messages thrown by JSLint and JSHint
http://linterrors.com
293 stars 35 forks source link

Unexpected '++' in a for loop #5

Closed filipjares closed 11 years ago

filipjares commented 11 years ago

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?

jamesallardice commented 11 years ago

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.

filipjares commented 11 years ago

I see. Thank you for your explanation.

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

XP1 commented 11 years ago

@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

filipjares commented 11 years ago

@XP1 Thank you for the reference!

Rahulsss commented 10 years ago

just do i += 1

like

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