JavaScript Set forEach()
Syntax & Examples

Set.forEach() method

The forEach() method of the Set class in JavaScript executes a provided function once for each value present in the Set object, in insertion order.


Syntax of Set.forEach()

There are 2 variations for the syntax of Set.forEach() method. They are:

1.
forEach(callbackFn)

Parameters

ParameterOptional/RequiredDescription
callbackFnrequiredFunction to execute for each element, taking three arguments: value, value again (for compatibility with Map), and the Set object.

This method calls callbackFn once for each value present in the Set object, in insertion order.

Returns value of type undefined.

2.
forEach(callbackFn, thisArg)

Parameters

ParameterOptional/RequiredDescription
callbackFnrequiredFunction to execute for each element, taking three arguments: value, value again (for compatibility with Map), and the Set object.
thisArgoptionalValue to use as this when executing callbackFn.

This method calls callbackFn once for each value present in the Set object, in insertion order. Uses thisArg as the this value for each invocation of callbackFn.

Returns value of type undefined.



✐ Examples

1 Using forEach() with a simple callback

In this example, we use the forEach() method to log each element of the Set to the console.

For example,

  1. We define a Set mySet with some values.
  2. We use the forEach() method with a callback function that logs each value to the console.

JavaScript Program

const mySet = new Set([1, 2, 3]);
mySet.forEach(value => console.log(value));

Output

1
2
3

2 Using forEach() with a callback and thisArg

In this example, we use the forEach() method with a callback function that logs each value and the this value to the console. We pass an object as thisArg.

For example,

  1. We define a Set mySet with some values.
  2. We define an object thisArg with a property prefix.
  3. We use the forEach() method with a callback function and pass thisArg as the second argument.
  4. The callback function logs the prefix and the value to the console.

JavaScript Program

const mySet = new Set(['a', 'b', 'c']);
const thisArg = { prefix: 'Value: ' };
mySet.forEach(function(value) {
  console.log(this.prefix + value);
}, thisArg);

Output

Value: a
Value: b
Value: c

3 Using forEach() to modify elements

In this example, we use the forEach() method to create a new array with modified elements from the Set.

For example,

  1. We define a Set mySet with some values.
  2. We create an empty array newArray.
  3. We use the forEach() method with a callback function that modifies each value and pushes it to newArray.
  4. We log newArray to the console.

JavaScript Program

const mySet = new Set([1, 2, 3]);
const newArray = [];
mySet.forEach(value => newArray.push(value * 2));
console.log(newArray);

Output

[2, 4, 6]

Summary

In this JavaScript tutorial, we learned about forEach() method of Set: the syntax and few working examples with output and detailed explanation for each example.