0

I'm trying to combine various code so I want to declare the start of i ahead of time.

OLD

var i,
  max_i = 4;


for (i = 2; i<= max_i; i += 1)  
{
  //things
}

NEW

var i = 2,
  max_i = 4;


for (; i<= max_i; i += 1)  
{
  //things
}

The problem is that I am getting various errors in JSLint

  • Expected ')' and instead saw '<='.
  • Expected ';' and instead saw 'i'.
  • Expected '{' and intead saw 'max_i'.

The code executes fine in both cases.

AllisonC
  • 2,973
  • 4
  • 29
  • 46

3 Answers3

1
for (i; i<= max_i; i += 1)  
{
  //things
}
Murad Sofiyev
  • 790
  • 1
  • 8
  • 25
0

JSLint helps you write better code, have faith in it, it's not about taste or else.

It complains because not initializing the for() is considered ugly (bad style).

You should initilize it as always. I would also go as far as to not define variables but declare them, and then initialize them as shown below. I would also use "use strict" nowadays. Not doing so is kind of unpleasant. The outer function is to avoid globals and non-function-scoped code:

/*jslint for */

(function () {
    "use strict";
    var i;
    var max_i;

    max_i = 4;
    for (i = 2; i <= max_i; i += 1)
    {
      // things
    }
}());
pid
  • 11,472
  • 6
  • 34
  • 63
-2

You can pre-declare and initialize i, but you must still set it as the loop variable in the loop configuration. And, it is clearer if you initialize it in the loop configuration, rather than above because debugging will be easier.

var i , max_i = 4;

for (i = 2; i <= max_i; i++) {
  //things
}

Also, it is better to always place the opening curly-brace (}) on the same line as the statement it defines as there are certain cases in JavaScript, where placing it on the next line down can cause bugs in the code due to automatic semi-colon insertion.

Lastly, to increment a value by one, you can use the ++ unaray operator, instead of +=1.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • 1. I think that's just a matter of coding style. I prefer them lined up. Never had issues with that. 2. If I use the ++ operator jslint gives an error, so I have changed it to i+=1. – AllisonC Jun 15 '17 at 15:22
  • "but you must still set it as the loop variable in the loop configuration" why **must**? `var i = 0; for(;;) {console.log(i++); if(i> 1) break;}` – Yury Tarabanko Jun 15 '17 at 15:23
  • 1
    @AllisonC As I stated in my answer, it is a common misconception that this is just a preference of coding style. It is a style that can lead to actual bugs in your code under the right circumstances. – Scott Marcus Jun 15 '17 at 15:23
  • @YuryTarabanko Because your example is nothing more than a `while` loop. To properly use a `for` loop, you would set it up to work properly. – Scott Marcus Jun 15 '17 at 15:24
  • @ScottMarcus, I'd like to see some sort of proof of that claim. In my 10+ years of coding I've never come across an issue like that. – AllisonC Jun 15 '17 at 15:25
  • And the why **must**? There is nothing except for coding style that makes me initialize loop variable in the loop itself. – Yury Tarabanko Jun 15 '17 at 15:26
  • 1
    @AllisonC [sigh] Sorry, that in your experience you haven't learned this important and true fact. In my experience, I've never broken my leg skiing. Then again, I have never skied. Nonetheless, it exists: https://stackoverflow.com/questions/3641519/why-does-a-results-vary-based-on-curly-brace-placement – Scott Marcus Jun 15 '17 at 15:30
  • @YuryTarabanko The must is because if you are going to actually use a `for` loop for its intended purpose, you need to configure it. Otherwise, as I stated, you are actually just writing a `while` loop. – Scott Marcus Jun 15 '17 at 15:32
  • @ScottMarcus, in the link you posted, I've never had that issue, I have written code like that that would result in automatic semicolon issues, so it's never been a problem for me. – AllisonC Jun 15 '17 at 15:36
  • No I'm writing for-loop. Just because I could rewrite it using `while` doesn't make it "not a for-loop". I could do it to every for-loop :) And again there is nothing other than code style that forces one to initialize loop variable inside for-loop. – Yury Tarabanko Jun 15 '17 at 15:38
  • @AllisonC Again, because you have never encountered that situation doesn't mean you never will. Programming using best-practices is all about preventing bugs in the future. It seems a bit ironic that you are very concerned about JSLint, but yet would ignore the advice of its creator (Doug Crockford), who is known in the JavaScript world as "the" guru of best-practices (JavaScript: The Good Parts, O'Reilly). – Scott Marcus Jun 15 '17 at 15:38
  • @YuryTarabanko I don't want to go round and round with you on this. If you write `for` but then set up your own mechanism for a control variable, then you are not using `for` correctly. Your code would fail a code review. – Scott Marcus Jun 15 '17 at 15:40
  • @ScottMarcus Neither do I. :) I have downvoted your answer bacause it is too opinionated. – Yury Tarabanko Jun 15 '17 at 15:43
  • @YuryTarabanko LOL, the original question is opinionated! Of course the answers will be. – Scott Marcus Jun 15 '17 at 15:43
  • @ScottMarcus, I'm not changing my coding style just for 1 possible case that I can just document and keep it in mind if that happens. Also, with JSLint, I am ignoring whitespace on purpose. – AllisonC Jun 15 '17 at 17:30
  • @AllisonC That's your call, but despite your opinion that this is really just a debate about "coding styles", it really isn't. There are many facets to JavaScript that are "allowed", but have come to be known universally as bad practices or anti-patterns. Choosing a practice that can cause bugs over one that will never cause a bug is a willful decision to use an anti-pattern, not a coding style choice. – Scott Marcus Jun 15 '17 at 18:04