13

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

I'm a big fan of Douglas Crockford and his excellent book, JavaScript: The Good Parts. I also use his JSLint tool on an hourly basis before checking in any code to our repository, as is good sense.

One thing I've noticed when running code through JSLint is its insistence that the ++ increment operator is somehow evil. I know I can turn certain rules off, but that's cheating ;). Crockford mentions his dislike on page 112 of JS: TGP...

In my own practice, I observed that when I used ++ and --, my code tended to be too tight, too tricky, too cryptic. So, as a matter of discipline, I don't use them any more. I think that as a result, my coding style has become cleaner.

That's all very lovely, but he doesn't give any examples of the way he codes them now. I assume he's doing something like...

var i;
i = 0;
i = i + 1;

Again, great, but I've got a few basic 'for loops' in my JS code, as I imagine many people have, and I've always used the standard syntax...

for (i = 0; i < myArray.length; i++) { 
    // Loop Stuff 
}

Am I missing something? What's the cleanest and/or best way to increment/decrement?

Community
  • 1
  • 1
Daniel Attfield
  • 2,024
  • 2
  • 24
  • 40

6 Answers6

15

I think this is rather controversial, and I personally stick to i++ in loops. Of cource, you can replace it with i = i + 1 in your loop statement, to be fully compliant to JSLint.

There aren't real alternatives to increment and decrement numerical values in JavaScript. You can often use Array.forEach() and/or Object.keys() in order to prevent numerical indexes.

b_erb
  • 20,932
  • 8
  • 55
  • 64
4

The only tricky thing I see in autoincrement/decrement operators (and I'm quite surprised that no one here pointed that out) is the difference between prefix (++i) and postfix (i++) versions. I also believe that autoincrement/decrement operators are slightly more efficient, in general. Besides this, I think the choice is based mainly on aesthetics, especially in javascript.

ascanio
  • 1,506
  • 1
  • 9
  • 18
3

to pass JSLint I now always do:

for (var i = 0; i < myArray.length; i+=1) { 
    // Loop Stuff 
}
Alvin
  • 10,308
  • 8
  • 37
  • 49
0

In C/C++ this is important, especially with arbitrary iterators and pre/post increment semantics. Also, random access vs forward-iterators.

In Javascript it's more about aesthetics than anything else. Unless you're dealing with strings. "1"+1 is "11".

I would say that ++i is an idiom that's worth being consistent with.

JSLint is wrong.

spraff
  • 32,570
  • 22
  • 121
  • 229
0

Personally, I see nothing wrong with the ++/-- operators for incrementing and decrementing. While something like i = i + 1; may be easier for new coders, the practice is not rocket science once you know what the operators stand for.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

Regarding loop, one original idea can be using such loop to avoid all kinds of numbers:

var a = [];
for (var i in "     ")
    a.push("Loop iteration here");
alert(a.join("\n"));

Meaning iterate over a string, there will be amount of iterations equal to the string length - 5 in the above example.

Live test case: http://jsfiddle.net/vTFDP/

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208