AppDividend
Latest Code Tutorials

Javascript Foreach Example

377

Get real time updates directly on you device, subscribe now.

Javascript Foreach Example is today’s leading topic. forEach() is the Array method that we can use to execute a function on each item in an array. It can only be used on the Javascript datatypes like Arrays, Maps, and Sets. It is the A function to be run for each item in the array. The forEach() executes the provided callback once for each element present in the array in ascending order.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript Foreach Example

Okay, first we will see the syntax of forEach method.

#Syntax of forEach

arr.forEach(function callback(currentValue[, index[, array]]) {
    //your iterator
}[, thisArg]);

The currentValue parameter is required. Other two index and array are optional.

Let us take an example of the forEach function.

Create one file called server.js and open that file inside VSCode. Write the following code in a server.js file.

// server.js

const apps = ['WhatsApp', 'Instagram', 'Facebook'];
const playStore = [];

apps.forEach(function(item){
  playStore.push(item)
});

console.log(playStore);

 

Javascript Foreach Example

So, here first we have taken an array of apps. Then we have iterate the array one by one using forEach and add the items one by one to the new array called playstore and display it.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Join the program

#Using For loop

We can do the same thing using the regular Javascript For loop.

// server.js

const apps = ['WhatsApp', 'Instagram', 'Facebook'];
const playStore = [];

for(let i = 0; i < apps.length; i++) {
  playStore.push(apps[i]);
}

console.log(playStore);

#Array.prototype.forEach()

The prototype constructor allows us to add new properties and methods to the Array() object itself. There are already some methods available in the Javascript, and one of them is forEach. When constructing the property, All arrays will be given the property, and its value, as default. When constructing a method, All arrays will have this method available. A prototype is a global object constructor which is available to all the Javascript objects. Let us take an example of that.

// server.js

function Square() {
  this.squareArray = [];
}
Square.prototype.multiply = function(arr) {
  arr.forEach(function(value) {
    this.squareArray.push(value * value);
  }, this);
};

const obj = new Square();
obj.multiply([2, 5, 9]);
console.log(obj.squareArray);

#for vs forEach()

Both the for loop and the forEach method allows you to loop over an array, but forEach has better readability than the for loop. Also, Fewer off-by-one errors with forEach() is a big advantage.

#JavaScript Array forEach() Method Main Tips

  • The forEach() applies a specified function for each of the items individually.
  • Introduced with ECMAScript 3.
  • This method only executes the specified function is an array item has a value.
  • Its return value is undefined.

Finally, Javascript Foreach Example is over.

 

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.