forEach() and each() in javascript with example and description

In this post,we will learn about 

forEach() and each() in javascript with example and description.

Description:

In my previous posts I have explained various topics like Fill DropDownList from JSON Array using jQuery, Anonymous Types In C# ,Sealed Class in C#, Continue keyword in C#.Net, Difference between string and StringBuilder in C#, Difference between a struct and a class in C#, Boxing & Unboxing in C# ,Dynamic Type In C#, Named Arguments In C#.

Now in this post, I will explain about how to get JavaScript .forEach() & jQuery .each() with example and description with appropriate example. There are two functions to work with an array on client-side – JavaScript .forEach() and jQuery .each().

JavaScript .forEach() Method

The .forEach() method of JavaScript executes a given function once for each element of the array.

var arr = ['a1', 'b2', 'c3'];    
arr.forEach(function(element) {  
    console.log(element);  
});

The above JavaScript code will print – ‘a1’, ‘b2’, & ‘c3’ in the console window.




jQuery .each() Method

jQuery has its own method to loop though an array. This is called jQuery Each Method – .each().

var arr = ['a1', 'b2', 'c3'];  
$.each(arr , function (index, value){  
  console.log(arr);   
});

The above jQuery code will print – ‘a1’, ‘b2’, & ‘c3’ in the console window.

If you are using jQuery, then prefer .each() method, else use .forEach().

Read :

Summary

You can also read about ASP.NET,C#.NET, JQUERY, AJAX.
I hope you get an idea about JavaScript .forEach() & jQuery .each().
I would like to have feedback for my blog.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website