9

Possible Duplicate:
Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
The “unexpected ++” error in jslint

jslint.com is giving me the error:

Unexpected '++'.

for this line:

for (i = 0; i < l; ++i) {

I tried i++ but no go.

Community
  • 1
  • 1

2 Answers2

12

JSLint does not like the increment and decrement operators. Replace it with i += 1 or add the plusplus: true directive to the top of your file (if you're not sure how to set JSLint directives, here's an example. They are set in a normal comment at the top of your file):

/*jslint plusplus: true */

From the JSLint docs:

The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces.

Completely ridiculous rule? You can make your own mind up...

MattDMo
  • 100,794
  • 21
  • 241
  • 231
James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 1
    Wow, jslint... you have disappointed me – TheZ Aug 16 '12 at 22:25
  • 4
    this is bad English..... faulty architecture in enabling to viruses – morph_master_flex Aug 16 '12 at 22:26
  • 2
    Yea, I agree. This is crap. `i++;` has been used since the beginning of time.. – Mike Christensen Aug 16 '12 at 22:27
  • Since even before computers, cavemen engraved it inside their graves – Doug Molineux Aug 16 '12 at 22:28
  • 1
    @MikeChristensen, `i++` has been *abused* since programmers started learning they could squish more into one line than was reasonable. The whole point of linting is to make code more expressive. If you know the algorithm is sound and it uses `++`, then ignore the warning. It **is** just a warning. – zzzzBov Aug 16 '12 at 22:30
  • Crockford can be a bit...*strict* in his recommendations. This is one of those guidelines I just don't understand. – jackwanders Aug 16 '12 at 22:30
  • 3
    It's because of confusing errors like this that I am gradually building up a comprehensive set of explanations of JSLint errors. Unfortunately this one isn't on there yet. It's all on GitHub, so if anyone feels like adding stuff in, have a look: http://jslinterrors.com/ – James Allardice Aug 16 '12 at 22:34
-3

Try: for (var i = 0; i < l; i++) {

If that doesn't work, see if i is defined by typing i in console and seeing the response.

Kpower
  • 1,237
  • 3
  • 11
  • 19