JavaScript

JavaScript forEach () Method [ 3 Best Examples ]

JavaScript forEach () is a method or function which calls the provided function for each element in an array in order. It is used to perform any operation on elements of the given array. Remember, it can not be executed for array elements without values.

JavaScript ForEach () Supported Browsers

The following browsers support JavaScript ForEach () method:

  • Google Chrome
  • Internet Explorer v9.0 or above
  • Mozilla Firefox v1.5 or above
  • Safari
  • Opera

JavaScript ForEach () Syntax

array.forEach(function(current_Value, index, arra), thisValue)

Where arra is optional parameter which is the current array to whom an element belongs. index is also optional parameter which is index of current element. currentValue is required parameter which is value of current element. thisValue is optional, it will be “undefined” if left empty, a value “this” should be passed.

You may be interested in learning Express JS.

JavaScript ForEach () Technical Details

  • JavaScript ForEach () is supported in ECMAScript 5.
  • It always returns undefined value by altering or without altering the provided array. Change in the array depends on the functionality of the argument function.

JavaScript ForEach () Examples

Example 1: Print Colors and associated index.

var colors = ["Blue", "Yellow", "Red"]
colors.forEach(myColors);

function myColors(color, index){
  document.getElementById("myDiv").innerHTML += color + ":"+ index + "<br>";
}

Example 2: Create a copy of array by multiplying each element’s value with 2

var values = [1, 2, 3]
var clone = [];

values.forEach(myClone);

myClone(value){
  clone.push(value * 2);
}

print(clone);

Example 3: Print sum of array elements using JavaScript forEach ()

var sum = 0;
var array = [155 , 45, 87, 8, 98, 14, 74];

array.forEach(mySum);

function mySum(num){
 sum += num;
    console.log(sum)
}

Sponsored by WebSoft IT Development Solutions Private Limited

Arslan ud Din Shafiq

Alibaba Cloud MVP, Alibaba Cloud Technical Author, Dzone MVB, Software Engineer, Software Developer, Software Designer, Web Engineer, Web Developer, Web Designer, Database Designer, Database Developer, Cloud Computing Specialist, Linux Expert, Servers, 3D Modeling, Blogger, Facebook Map Editor, Google Map Editor

Share
Published by
Arslan ud Din Shafiq

Recent Posts

How To Set Up Secure Nginx Server Blocks on Ubuntu 22.04

NGINX Server Nginx, a popular open-source web server, excels at handling high traffic websites efficiently.… Read More

5 days ago

The Web Server Showdown: Nginx vs. Apache, LiteSpeed, Caddy, and Beyond

In the realm of web hosting, choosing the right web server is paramount. It acts… Read More

5 days ago

Linear guidance systems

Are indispensable for ensuring smooth, precise linear motion in many industrial applications. Whether in robotics,… Read More

2 months ago

Cyber Attack Statistics – Identifying Vulnerabilities and Strengthening Defenses

Cyber attacks are becoming more frequent, complex, and damaging. They can disrupt critical operations and… Read More

3 months ago

Empowering Cybersecurity in 2024 with XDR for Comprehensive Threat Detection and Response

With the rise of new threats and the increasing complexity of IT environments, organizations need… Read More

3 months ago

Facade Design Pattern: Simplifying Complex Systems

1. Introduction In software design, managing complex systems can be challenging. The Facade Design Pattern… Read More

5 months ago