Recommended Free Tools
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Pass a second parameter to the forEach callback: the first parameter is the current item, and the second is its zero-based index.
const items = ["apple", "banana", "cherry"];
items.forEach((item, index) => {
console.log(index, item);
});
This logs 0 apple, 1 banana, and 2 cherry. TypeScript documents the callback parameters in this order: array element, index, then the containing array (TypeScript Handbook: Type Compatibility).
Use the second callback parameter
Name the second parameter index or i. TypeScript infers its type for an array callback, so you usually do not need to annotate it.
Free tools Windows power users keep installed
One-click scans. No signup required.
const items = ["apple", "banana", "cherry"];
items.forEach((item, index) => {
console.log(`${index}: ${item}`);
});
The callback receives the current element first and its zero-based position second. Its optional third parameter is the array being traversed:
#1 Best Overall
items.forEach((item, index, array) => {
console.log(array[index] === item);
});
If you only need the item, you can omit the other parameters; callback parameters you do not use can be ignored.
When to use a different loop
Use a counted for loop for explicit control
If you need to manage the index yourself or control the loop flow, use a counted loop:
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
for (let index = 0; index < items.length; index++) {
const item = items[index];
console.log(index, item);
}
With TypeScript’s noUncheckedIndexedAccess option enabled, an expression such as items[index] is checked as potentially undefined. The callback form passes the current element directly. See the TypeScript 4.1 release notes for the option.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteUse for...of when you only need values
A for...of loop yields values, not their indexes. It is suitable when the index is unnecessary.
Do not use for...in for an array index
A for...in loop visits keys. For arrays, those keys are strings, so it is not the same as receiving a numeric position in a forEach callback. TypeScript describes for...in as a way to inspect object properties; see Iterators and Generators.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.

