Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

forEach: an effect for every existing element

forEach calls a callback for existing elements, skips holes, and always returns undefined.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

array.forEach(callbackFn[, thisArg])

array.forEach(callbackFn[, thisArg]) calls callbackFn(value, index, array) in ascending order for existing indexes. Callback results are ignored, the method returns undefined, and it has no short circuit or normal break.

Length is captured before iteration, holes are skipped, and changes to later values can be observed. New indexes beyond initial length are not visited. A non-callable callback or null/undefined receiver throws TypeError. The method does not await an async callback; use a loop for sequential await. The example shows the sum, indexes, and undefined result.

Examples

const source = [1, , 3];
const visited = []; let sum = 0;
const returned = source.forEach((value, index) => { visited.push(index); sum += value; });
console.log(sum, visited.join(","));
console.log(returned);

Result

4 0,2
undefined

Sources