Set.prototype.forEach()

set.forEach(callbackFn, thisArg)
Returns: undefined · Updated March 13, 2026 · Map and Set
set foreach iteration

The forEach() method executes a provided function once for each value in the Set.

Syntax

set.forEach(callbackFn, thisArg)

Parameters

ParameterTypeDefaultDescription
callbackFnfunctionFunction executed for each value
valueanyThe current value
valueanySame as first argument (for Map compatibility)
setSetThe Set being iterated
thisArganyundefinedValue to use as this

The callbackFn receives three arguments (the second is the same as the first for Set compatibility with Map):

Examples

Basic usage

const numbers = new Set([1, 2, 3]);

numbers.forEach((value, sameValue, set) => {
  console.log(`Value: ${value}, Set size: ${set.size}`);
});
// Value: 1, Set size: 3
// Value: 2, Set size: 3
// Value: 3, Set size: 3

Using thisArg

const multiplier = {
  factor: 10
};

const nums = new Set([1, 2, 3]);

nums.forEach(function(value) {
  console.log(value * this.factor);
}, multiplier);
// 10
// 20
// 30

Processing unique values

const duplicates = [1, 2, 2, 3, 3, 3, 4, 4, 5];
const uniqueSet = new Set(duplicates);

const doubled = [Global_Objects::eval];
uniqueSet.forEach(value => {
  doubled.push(value * 2);
});
console.log(doubled);
// [2, 4, 6, 8, 10]

See Also