Open In App

JavaScript Set forEach() Method

Last Updated : 30 May, 2023
Like Article

The set.forEach() method is used to execute the function which is taken as a parameter and applied for each value in the set, in insertion order.


forEach(function(value, key, set) { 
    /* ... */ 
}, thisArg)


  • Callback function: The function will be executed for each value and it takes three arguments.
  • Value, Key: Value should be the current element that is processed. The set does not contain the key, the value is passed in place of the key.
  • set: This is the object in which forEach() was applied. 
  • thisArg: Value used as this when the callback function is called.

Return Value: This method returns undefined.

Example 1: In this example, we will see the use of the forEach() method.


function setValue(value1, value2, mySet) {
    console.log(`s[${value1}] = ${value2}`);
new Set(['Chicago', 'California', undefined])


s[Chicago] = Chicago
s[California] = California
s[undefined] = undefined  

Example 2: In this example, we will see the use of the forEach() method to display the value of Set. 


let fruits = new Set();
function display(i, set) {



We have a complete list of Javascript Set methods, to check those please go through the Sets in JavaScript article.

Supported Browser:

  • Chrome 38
  • Edge 12
  • Firefox 25
  • Opera 25
  • Safari 8

Previous Article
Next Article

Similar Reads

JavaScript typedArray.forEach() Method
The typedArray.forEach() is an inbuilt function in JavaScript which executes a given function once for each given typedArray element. Syntax: typedarray.forEach(callback) Parameter: It accepts a parameter "callback" function which produces each element of the new typedArray and this function takes three parameters that are specified below- currentV
1 min read
How to stop forEach() method in JavaScript ?
Stopping a forEach() loop seems almost like an impossible task but here are a few tricks by which we might do it. Sample example using forEach(): var sum = 0; var number = [90, 4, 22, 48]; number.forEach(myFunction); function myFunction(item) { sum += item; } console.log(sum); Tricks to stop forEach() loop: Method 1:The following method demonstrate
2 min read
JavaScript Map forEach() Method
JavaScript Map.forEach method is used to loop over the map with the given function and executes the given function over each key-value pair. Syntax: myMap.forEach(callback, value, key, thisArg)Parameters: This method accepts four parameters as mentioned above and described below: callback: This is the function that executes on each function
2 min read
JavaScript Array forEach() Method
The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array. Syntax:array.forEach(callback(element, index, arr), thisValue);Parameters: This method accepts five parameters as mentioned above and described below: callback: This par
2 min read
What is forEach() method in JavaScript ?
The forEach() method in JavaScript is used to iterate over the elements of an array and execute a provided callback function once for each element. It provides a convenient way to perform operations on each element of an array without the need for a traditional for loop. Syntax:array.forEach(callback(element, index, arr), thisValue);Parameters:call
2 min read
How to Change Values in an Array when Doing foreach Loop in JavaScript ?
The forEach loop in JavaScript is one of the several ways to iterate through an array. ForEach method is different from all other looping methods as it passes a callback function for each element in the array. So, the possible approach to change the values in the array when doing foreach method in JavaScript, the method will not directly change the
3 min read
How to iterate an Array using forEach Loop in JavaScript?
Iterating through an array in JavaScript can be done in multiple ways. One of the most common methods is using the traditional for loop. In JavaScript, we have another modern approach to iterate the array which is by using the forEach method. This method is different from the traditional approach as it uses a functional approach. This approach prov
2 min read
How to loop through HTML elements without using forEach() loop in JavaScript ?
In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Using the for loopUsing the While loopUsing the 'for.....of' statementApproach 1: Using the for loopThe HTML elements can be iterated by using the regular JavaScript for loop. The number of el
3 min read
How to use async/await with forEach loop in JavaScript ?
Asynchronous is popular nowadays because it gives functionality of allowing multiple tasks to be executed at the same time (simultaneously) which helps to increase the productivity and efficiency of code. Async/await is used to write asynchronous code. In JavaScript, we use the looping technique to traverse the array with the help of forEach loop.
2 min read
Difference between forEach() and map() loop in JavaScript
When we work with an array, it is an essential step to iterate on the array to access elements and perform some kind of functionality on those elements to accomplish any task. For this loops like ForEach() and Map() are used. In this article, we are going to learn the difference between the forEach() and map() loops in JavaScript. JavaScript forEac
3 min read