Get the Current Array Index in JavaScript's forEach()

Introduction

An Array in JavaScript is a standardized, built-in object used to store multiple objects under the same name. In simple terms, you can look at the Array object as an array in any other programming language. It is essentially a class that encapsulates an array (an ordered list of values) and all necessary methods you might need to perform array operations.

The fact every array is ordered means that the place of each element is of great importance. In fact, you can't even extract the value of an element if you don't know its place in the original array.

During iteration, the index of the current element may be relevant to you. This is very straightforward to achieve using the index parameter of the forEach() method.

In this article, we'll see how to get the current element's array index in JavaScript using the forEach() method.

forEach() Method Basics

The forEach() is a method of the Array class. You can use it to loop through an array and perform a certain operation on each of its elements - though, map() is more commonly used for this purpose. You can customize the performed operations using a callback function - a function passed as a parameter of the forEach() method. A callback function is a simple function that defines the operation to be performed on a single element, and the forEach() method makes sure it will be performed on each element of an array.

The forEach() method has a pretty straightforward syntax:

forEach(callback(currentElement, index, arr), thisValue);

As we've stated before, the forEach() method accepts a callback function that runs on each entry in the array. This callback function takes in three arguments, of which we are only concerned about two - the currentElement which is a required parameter, and its index which is the position of the currentElement in the array.

How to Get the Index of the Current Element Using forEach() Method

Suppose we have an array of students, and would like to access the index of each element as we're processing them. We'll make use of the index argument, and simply print it out:

const students = ["Mark", "Jane", "John", "Sarah"];

students.forEach((student, index) => {
    console.log(`The index for ${student} is ${index}`);
});

Output:

"The index for Mark is 0"
"The index for Jane is 1"
"The index for John is 2"
"The index for Sarah is 3"

Conclusion

In this short article, we've explained how to use the index parameter to get the current array index of elements in an Array using the forEach() method.

Note: If you need to dig deeper into the forEach() method in JavaScript, read our Guide to JavaScript's forEach().

Last Updated: April 6th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Joel OlawanleAuthor

Frontend Developer & Technical Writer

Project

React State Management with Redux and Redux-Toolkit

# javascript# React

Coordinating state and keeping components in sync can be tricky. If components rely on the same data but do not communicate with each other when...

David Landup
Uchechukwu Azubuko
Details

Getting Started with AWS in Node.js

Build the foundation you'll need to provision, deploy, and run Node.js applications in the AWS cloud. Learn Lambda, EC2, S3, SQS, and more!

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms