"4. for loop backwards" Greater than?



The question I have was also asked here.

var vacationSpots = ['UK','Japan','Washington State'];

for (var i = vacationSpots.length - 1; i >= 0; i--) {
  console.log('I would like to visit ' + vacationSpots[i]);

Why do we use " >= " instead of " <= " or " = " ? The start conditions is 2, which is >= 0, why does it not stop immediately?

The only comment on the linked thread assumes to OP is simply mistaken, but this is exactly what I see. What am I missing?


Consider counting down from 10 to 1. From which direction are we approaching the lower limit?

The limit, or stop value is less than the iterator value, which means that value is greater than the stop value.


I don't understand this.

The stop value is >= 0. The iterator value is -1, right? All infinite possibilities of the stop value are still greater than the iterator value.

Also, I don't understand why how this answers the question.

The code says to stop whenever the iterator reaches >= 0. This criterion is never unmet.


- 1 is the direction, not the iterator value. The iterator value will be the index.


I suppose I am having trouble with vocabulary, here. That isn't as important, I think, as making sense of the logic. Though, the former may be a prerequisite of the latter.

At the beginning of the loop, i should be = 2. The loop should stop when i >= 0. This condition is met from the beginning.

Is i = index = iterator value? Interchangeable in this context?

Thanks for your replies!

edit: typos


I have identified the confusion.

The course teaches that the for loop has 4 parts:

  1. Start condition
    2. Stop condition
  2. Iterator
  3. Code block

The wording here is just misleading. Stop Condition suggests that it will tell the loop when to stop. This is more like a continue condition.

I assume this is already the generally accepted term, but for someone new to coding it just looks backwards.


Correct. The arguments of a for loop are a value or state; a while condition; and, a state change.

for (var i = 0; i <= 10; i++) { // code block }
         state   while  change

The change in state is like the invisible last line of the code block, before returning to for.

In the earlier question we were going in reverse.

for (var i = 10; i >= 0; i--) { // log i }

When the pseudo code is replaced with working JS it will log all the numbers from 10 to 0, inclusive. The while in this case is that the iterator value is greater or equal to zero.


Notice that this prints out 11 values in each case? Remove the = from each conditional expression and they will only print 10 values. A little something to keep in the back of one's mind.

When dealing with arrays, which are zero-indexed lists, essentially. the less than or greater than, depending on direction, will not be out of range.

var list = [1, 2, 3, 4]
for (var i = 0; i < list.length; i++) { // log list[i] }        // 1 2 3 4
for (var i = list.length; i > 0; i--) { // log list[i - 1] }    // 4 3 2 1


Sorry, I seem to have skipped this question. Generally speaking, yes. Trouble with being self-taught is that you never really learn the vernacular in the same way as one would on a scholastic learning path. As a result my terminology could well fall into question, I admit. I agree that the logic is more important.

In my mind the iterator value is the monitored state. It is for the most part a number, but not exclusively so in a more esoteric exploration of for. Not for this discussion, mind. As a number, if used as an index it is an integer, but if used as a sequential value it could be a float.


var i = 1;
for ( i; i < 2; i += 0.05 ) {

will print all the values from 1 to 1.95.

If I am wrong, then do correct me, please. Or at least we can agree which component is the iterator when I describe it. When I think state change what comes up is, increment, decrement, add, subtract, multiply, divide, shift or toggle. I do not find a way to relate this to a group of iterators. Hence the connection to the favored i as the variable, in many one-off loops as the iterator variable which references the iterator value, the one dynamic piece of the puzzle.


Thanks for these detailed responses. As you say, learning on my own is leaving out a lot of the meat, so this is very helpful.

It's also helpful just to see slightly different approaches to the same code. The learning exercise does the count-down for loop by putting -1 on the state, whereas you put it in the block. Small difference, but it gets the mind working on looking at coding from different angles.

Anyways, thanks a lot for your time and explanation!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.