1

Why i<array.length rather than i=array.length When I originally wrote my code, I told the for loop to go through the length of the array. I defined the for loop as (var i = 0; i=array.length; i++). That created an infinite loop. The way to fix it is to set i<array.length. However, can somebody explain to me why? It feels as if the loop should continue to the end of the array. If I set it to less than the length of the array, how do I know that it has checked all of the numbers?

Thanks!

Note: Here is my code.

var array = [3, 6, 2, 56, 32, 5, 89, 32]; var largest = 0; for (var i = 0; i largest) { largest = array[i] } } console.log(largest);

4
  • 4
    i=array.length is not even a comparison? Did you mean i==array.length? Commented May 22, 2019 at 13:38
  • because indexing starts at 0 Commented May 22, 2019 at 13:39
  • You should have a look at the doc: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 22, 2019 at 13:39
  • This seems to be asking about why an idiom came to be popular. That invites a lot of speculation. If that's not what you're asking, maybe rephrase to be more specific. Commented May 22, 2019 at 13:43

4 Answers 4

2

if you write i=array.length then you SET length to i - not COMPARE (it is allways true (or cast to true - except if number is zero). To compare you need write i!=array.length or i!==array.length. Second thing if you use i as index then is beter to use i<array.length because array elements are indexed from 0 to length-1 and value i greater equal length will newer occure.

let array = ['a','b','c']; let i=3; console.log('!==', i!==array.length ); console.log('!=', i!=array.length ); console.log('<', i<array.length ); console.log('=', i=array.length );

Sign up to request clarification or add additional context in comments.

1 Comment

Don't you want !== instead of === for a before end of loop check?
1

Because

for (i = 0; i < array.length; i++)

means:

i = 0 i starts at 0 at the beginning of the for-loop

i < array.length as long as i is less than array.length, we continue looping

i++ after each loop we increment i by 1 (i.e. i = i + 1)

When you replaced i < array.length by i = array.length, you are saying let i = array.length which is setting the value i to array.length. So long as the statement is true it will continue looping. That's why it's an infinite loop.

But I guess that was a typo, you really meant i == array.length. Still doesn't work, because all arrays start with index 0 (which is why i = 0 to start). This means they end at index of array.length - 1.

Example:

arr = ["a","b","c"]; //arr[2] = "c", but arr.length = 3 

So we if loop through arr, we need to stop at index 2, not 3

Comments

0

the second argument in for defines the condition under which the loop should continue/stop. The reason why i<array.length is correct is that arrays in javascript are 0 based. So if you try to get array[array.length], it is out of bounds by definition

Comments

0

When setting i=array.length your loop condition is going to evaluate what is in i. If the length of the array is greater than 0 it will be a truthy value, resulting in an infinite loop. If it is 0, the loop body (statement) won't execute.

You are overwriting your step-variable -or- iterator variable, i, which is not what you want to do. If anything you can: for (var i=0,n=array.length; i<n; i++); however, storing the array length in a variable is no longer necessary for performance optimization (I think browsers now optimize when converting JS to bytecode.


Refer to MDN resource on for-loops for more information; here's a snippet:

for ([initialization]; [condition]; [final-expression]) statement 

initialization

An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with var or let keywords. Variables declared with var are not local to the loop, i.e. they are in the same scope the for loop is in. Variables declared with let are local to the statement.

The result of this expression is discarded.

condition

An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

final-expression

An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.

statement

A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements. To execute no statement within the loop, use an empty statement (;).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.