JavaScript Map forEach() method

The JavaScript map forEach() method is used to execute the specified function once for each key/value pair.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mapObj.forEach(callback)
mapObj.forEach(callback)
mapObj.forEach(callback)

Parameters:
callback: It refers to the function which has to be executed.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var hello=new Map();
hello.set(1,"GOOD MORNING");
hello.set(2,"GOOD AFTERNOON");
hello.set(3,"GOOD EVENING");
hello.set(4,"GOOD NIGHT");
document.writeln("Elements are:"+"<br>");
function print(elements)
{
document.writeln(elements+"<br>");
}
hello.forEach(print);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var hello=new Map(); hello.set(1,"GOOD MORNING"); hello.set(2,"GOOD AFTERNOON"); hello.set(3,"GOOD EVENING"); hello.set(4,"GOOD NIGHT"); document.writeln("Elements are:"+"<br>"); function print(elements) { document.writeln(elements+"<br>"); } hello.forEach(print); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var hello=new Map();
hello.set(1,"GOOD MORNING");
hello.set(2,"GOOD AFTERNOON");
hello.set(3,"GOOD EVENING");
hello.set(4,"GOOD NIGHT");
document.writeln("Elements are:"+"<br>");
function print(elements)
{
document.writeln(elements+"<br>");
}
hello.forEach(print);
</script>
</body>
</html>