0

I have created a for loop in JavaScript which looks like the following:

for (var i = 0; i < element[child].length; i++) {
    ...some code...
}

However when I run JSLint on it, it complains that the 'var i' should be moved to the beginning of the function (which seems odd to me to do) and it also complains that the ++ on the 'i++' is unexpected. It is this part that I really find weird - why is the ++ unexpected and what should it be replaced with?

user5013
  • 981
  • 4
  • 10
  • 21
  • Douglas Crockford (who wrote JSLint) prefers `i = i + 1` for some reason. You can toggle the option to allow `i++` using the `plusplus: true` option. Declaring the var at the top of the function is most likely to do with hoisting. I don't know what the JSLint option for that it tho. – Andy Oct 15 '13 at 16:34
  • While I think of it, I actually find hoisting the variable declarations to be really handy if I'm writing a complicated bit of code. When I see that I have declared a large number of them, I can start to check whether the function needs to be broken down into smaller components. – Andy Oct 15 '13 at 16:36

3 Answers3

1

The variables should be declared at the top of the function because that's what JavaScript ends up doing anyway (it's known as "variable hoisting"), so JSLint thinks it's clear to always have them there.

Regarding i++, it wants you to use i += 1. From 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. Also, preincrement/postincrement confusion can produce off-by-one errors that are extremely difficult to diagnose. There is a plusplus option that allows the use of these operators.

In short, JSLint believes it's evil, which is controversial. See also: http://jslinterrors.com/unexpected-plus-plus/

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Is there a reason why ++ is frowned upon by Douglas Crockfor? What kind of errors does he think this can this lead too? – user5013 Oct 15 '13 at 16:40
  • He mostly thinks it may lead to (logical) confusion. Also, it might lead to errors (see http://stackoverflow.com/questions/14325011/javascript-and-automatic-semicolon-insertion). I personally like those operators, Crockford is too radical. – bfavaretto Oct 15 '13 at 17:07
0

It is saying to move the var i out. It should be something like:

function foo() {
    "use strict";
    var i, element, child;

    //...set element/child...

    for (i = 0; i < element[child].length; i += 1) {
        //...some code...
    }
}

There is a nice post covering the ++ topic here: Why avoid increment ("++") and decrement ("--") operators in JavaScript?

Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Honestly, I think your code is fine and the issue is more with JSLint being too picky. When I run your code through JSHint I get no problems: http://www.jshint.com/

Remember that JSLint will often be very picky even if something still works correctly. You just have to know when to take its advice and when to ignore it :)

cardern
  • 715
  • 5
  • 18