# How to get the current index in a forEach loop?

**URL:** <https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992>\
**Category:** Beginners\
**Created:** [February 26, 2024, 9:28am UTC](https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992 "2024-02-26T09:28:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amundsen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/amundsen/32/2497_2.png) [@amundsen](https://discourse.processing.org/u/amundsen)\
**Post date:** [February 26, 2024, 9:28am UTC](https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992/1 "2024-02-26T09:28:44Z")

</div>

Hello,

Is it possible to have the current index of the item currently handled inside a forEach loop?

Thank you.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 26, 2024, 9:58am UTC](https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992/2 "2024-02-26T09:58:33Z")

</div>

Method **forEach()** passes 3 args to its callback function: _value_, _index_ and _array_:

> **[Array.prototype.forEach() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#callbackfn)**
>
> The forEach() method of Array instances executes a provided function once
> for each array element.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 26, 2024, 11:07am UTC](https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992/3 "2024-02-26T11:07:11Z")

</div>

or just manually

```auto
int i=0; 

for( String s1 : list ) {

...
i++; 

}

println(i);

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 26, 2024, 12:05pm UTC](https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992/4 "2024-02-26T12:05:19Z")

</div>

```auto
data.forEach(function((value, index, array){ 
    // implementation for each element in the array
});

```

If you don’t need to alter the array then use

```auto
data.forEach(function((value, index){ 
    // implementation for each element in the array
});

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 26, 2024, 12:07pm UTC](https://discourse.processing.org/t/how-to-get-the-current-index-in-a-foreach-loop/43992/5 "2024-02-26T12:07:29Z")

</div>

Using the fat-arrow lambda syntax:  
`array.forEach( (val, idx, arr) => console.log(val, idx, arr == array) );`

> **[Arrow function expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)**
>
> An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:
