Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Set.prototype.forEach

Calls a function for each value of a Set.

Available since: ES2015

Syntax

set.forEach(callbackFn)

Parameters

ParameterTypeDescription
callbackFnFunctionA function that receives each value.

Return value

undefinedReturns nothing.

Examples

Iterating over values:

const colors = new Set(['red', 'green']);
 
colors.forEach((color) => console.log(color));

Output:

red
green

Notes

People usually write for (const value of set) instead — it lets you use break.

See also

  • Map.prototype.forEach — Calls a function for each entry of a Map.
  • for...of — Loops over the values of arrays, strings, Maps, Sets and other iterables.

In the tutorial

  • Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.

Thanks for reading!

Donate to the site — we work hard for you.