Set forEach() JavaScript

The JavaScript Set forEach() method executes the specified function once for each value.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
setObj.forEach(callback(currentValue, currentKey, Set))
{
// code to be executed
}
setObj.forEach(callback(currentValue, currentKey, Set)) { // code to be executed }
setObj.forEach(callback(currentValue, currentKey, Set))
{ 
  // code to be executed 
}

Parameters:
callback: It represents the function to be executed for each element, taking three arguments: (currentValue, currentKey, and set).

thisArg: It represents this keyword for the function.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var jewels = new Set();
jewels.add("DIAMOND").add("GOLD").add("PLATINUM").add("SILVER");
var gems= jewels.entries();
jewels.forEach(function display(i,set)
{
document.writeln(i+"<br>");
})
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var jewels = new Set(); jewels.add("DIAMOND").add("GOLD").add("PLATINUM").add("SILVER"); var gems= jewels.entries(); jewels.forEach(function display(i,set) { document.writeln(i+"<br>"); }) </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var jewels = new Set();
jewels.add("DIAMOND").add("GOLD").add("PLATINUM").add("SILVER");
var gems= jewels.entries();
jewels.forEach(function display(i,set)
{
document.writeln(i+"<br>");
})
</script>
</body>
</html>