The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Use the callback’s second parameter: the first is the current item, and the second is its zero-based index.
Get the index with the second callback parameter
const items = ["apple", "banana", "cherry"];
items.forEach((item, index) => {
console.log(index, item);
});
This logs 0 apple, 1 banana, and 2 cherry. The TypeScript documentation specifies the forEach callback parameters in this order: array element, index, and containing array (TypeScript: Type Compatibility).
As an Amazon Associate I earn from qualifying purchases.
Name the second parameter index or i, whichever makes the code clearest. You can omit it when you only need the item:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
items.forEach((item) => {
console.log(item);
});
Use the third parameter when you need the array
The callback’s optional third parameter is the array being traversed. For example, it can be useful when a callback needs to refer to the current array as well as the item and its position:
#1 Best Overall
items.forEach((item, index, array) => {
console.log(array[index], item);
});
Choose a different loop only when it fits the task
Use a counted for loop for explicit index control
If you need to manage the index yourself or control loop flow, use a counted loop:
for (let index = 0; index < items.length; index++) {
console.log(index, items[index]);
}
With TypeScript’s noUncheckedIndexedAccess option enabled, an indexed array access may be treated as possibly undefined. The forEach callback instead passes the current element directly. See the TypeScript 4.1 release notes for this option.
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
Use for...of when you only need values
A for...of loop gives you each value, but not its index. It is a good fit when position does not matter.
Crashes, 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 minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallDo not confuse for...in keys with the index parameter
for...in iterates keys; for an array, those keys are strings. It is intended for inspecting object properties, not as a substitute for the numeric index supplied to a forEach callback. The TypeScript documentation describes these loop behaviors in 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.




