# For Loops: "Less than" or "Less than or equal to"?

**URL:** <https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 30, 2018, 3:35am UTC](https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808 "2018-05-30T03:35:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![camillevo](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@camillevo](https://forum.freecodecamp.org/u/camillevo)\
**Post date:** [May 30, 2018, 3:35am UTC](https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808/1 "2018-05-30T03:35:36Z")

</div>

Hello, this is a simple question but one that’s been bugging me while doing the Profile Lookup challenge.  
In a for loop ( for ( var i = 0; i \< contacts.length; i++)), why is the “i” stopped when it’s less than the length of the array and not less than or equal to (\<=)? Shouldn’t the for loop continue until the end of the array, not before it ends?

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [May 30, 2018, 3:38am UTC](https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808/2 "2018-05-30T03:38:22Z")

</div>

The index of the last element in any array is always its length minus 1. With `i < contacts.length`, you’re telling the loop to only count up to the integer before that length, which is the index of the last element.

---

<div class="post-metadata">

**Author:** ![camillevo](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@camillevo](https://forum.freecodecamp.org/u/camillevo)\
**Post date:** [May 30, 2018, 3:57am UTC](https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808/3 "2018-05-30T03:57:13Z")

</div>

I see where my confusion came from! On the challenge “Iterate Through an Array with a For Loop”, I accidentally put “ourArr” instead of “myArr” and then thought I was clever to use a \<=. The challenge passed, but only because ourArr just happened to be one parameter shorter than myArr! Thank you for your help!

// Example  
var ourArr = [9, 10, 11, 12];  
var ourTotal = 0;

for (var i = 0; i \< ourArr.length; i++) {  
ourTotal += ourArr[i];  
}

// Setup  
var myArr = [2, 3, 4, 5, 6];

// Only change code below this line  
var total = 0;

for (var i = 0; i \<= ourArr.length; i++) {  
total += myArr[i];  
}

---

<div class="post-metadata">

**Author:** ![sally1](https://avatars.discourse-cdn.com/v4/letter/s/a587f6/32.png) [@sally1](https://forum.freecodecamp.org/u/sally1)\
**Post date:** [May 31, 2018, 2:51am UTC](https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808/4 "2018-05-31T02:51:30Z")

</div>

Arrays start with index 0 , so if you have 10 elements in your array, the last index would be 9; that’s why the last index of an array is always length -1 . Hope that helps clarify.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:51am UTC](https://forum.freecodecamp.org/t/for-loops-less-than-or-less-than-or-equal-to/193808/5 "2021-02-02T11:51:31Z")

</div>

Closed on February 2, 2021, 11:51am UTC


