For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/fp/forEach.md.

forEach (Functional Programming) es-toolkit

Creates a function that runs a callback for each value and returns the input array. Use it with pipe.

const result = pipe(array, forEach(callback));
Info

This helper is specific to es-toolkit/fp. Use it when you want this operation as part of a pipe pipeline.

Usage

forEach is useful as a side-effect step in a pipeline. It calls callback for each value and passes the original array through. Inside pipe, it is lazy-capable when adjacent lazy operations can stop early.

import { forEach, pipe } from 'es-toolkit/fp';

const values: number[] = [];

pipe(
  [1, 2, 3],
  forEach(value => values.push(value))
); // => [1, 2, 3]
values; // => [1, 2, 3]

Parameters

callback(value: T, index: number) => voidrequired

The function to run for each value.

Returns

A function that returns the same array after running the callback.

(array: readonly T[]) => readonly T[]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.