I still haven't fully understood the different between the for loops and forEach method. When exactly to use for loops and when exactly to use forEach. Does forEach work only on a certain type of objects?
Thank you
I still haven't fully understood the different between the for loops and forEach method. When exactly to use for loops and when exactly to use forEach. Does forEach work only on a certain type of objects?
Thank you
For the most part it comes down to style. You can essentially do the same things with a for(...){...} loop as you can with a forEach(...) loop. Natively, javascript only supports forEach on objects that are instances of Array or Map.
Let's start with forEach for Arrays. This forEach takes a callback function as an argument. This callback will be run once for every item in the array you call it on. The signature for the callback looks like this:
/** * value - the current value * index - the current index * array - the array being iterated over (i.e. the array you called forEach on) */ function (value, index, array) {...} And here's an example:
var sum = 0; var nums = [1, 2, 3, 4, 5]; // This loop will sum up all the values in the 'nums' array... nums.forEach(function (value, index, array) { sum += value; }); //... So will this... sum = 0; nums.forEach(function (value, index, array) { sum += nums[index]; }); //... And this. sum = 0; nums.forEach(function (value, index, array) { sum += array[index]; }); This would be the same loop, but as a for loop:
var sum = 0; var nums = [1, 2, 3, 4, 5]; for(var i = 0; i < nums.length; i++) { sum += nums[i]; } It must be noted that, while you can break out of for loops early, you cannot do so in a forEach loop.
There are also slight performance differences when it comes to for loops vs. forEach loops. for loops are usually faster, but to some people they appear ugly and hard to read. If you want to read more, take a look at David Tang's article covering for loops vs forEach loops on Arrays
forEach loops on a Map look a bit different, but the idea is the same. The callback signature for this will be:
/** * value - the current value * key - the current key * map - the map being traversed (i.e. the map you called forEach on) */ function (value, key, map) {...} Here's an example:
var totalMoney = 0; var people = new Map([['Jan', 100], ['Michael', 50], ['Dwight', 30], ['Jim', 20], ['Pam', 20]]); // This loop will sum up all the money that 'Jan', 'Michael', 'Dwight', 'Jim', and 'Pam' possess... people.forEach(function (value, key, map) { totalMoney += value; }); //... As will this... totalMoney = 0; people.forEach(function (value, key, map) { totalMoney += people.get(key); }); //... And this. totalMoney = 0; people.forEach(function (value, key, map) { totalMoney += map.get(key); }); Use forEach when you want to run a function on all elements of an array.
const numbers = [ 1, 2, 3] const newNumbers = [] const addFive = (number)=>newNumbers.push(number + 5) numbers.forEach(addFive) console.log(newNumbers) http://jsbin.com/johenohace/edit?js
NOTE: There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool. Use a plain loop instead.
Take the worksheets in a workbook as an example. All worksheets are members of the Worksheets collection. You can use a For Each loop to call up each member of the collection. Similarly, your workbook is a member of the Workbooks collection which comprises all currently open workbooks. The advantage of using For Each is that you don't need to know how many members there will be. Every collection has a Count property. Worksheets.Count will tell you how many worksheets there are in the ActiveWorkbook, Workbooks.Count will inform you of how many workbooks are currently open.
Each member of a collection also has an index number. Note that the same number doesn't necessarily always identify the same item. In Excel the first tab on the left is always Worksheets(1). Add another tab on the left and the new tab will assume that number. For most collections the index number can be replaced with a name. Worksheets("Sheet1") may be the same as Worksheets(1), depending upon the location of the tab.
For i = 1 to Worksheets.Count Set Ws = Worksheets(i) would have the same effect as For Each Ws in Worksheets but the latter appears to be more efficient, and it is said to be much faster to loop through a collection than to call up individual members of it.
Of course, the collection which enables to better speed and easier access must be maintained. The application is doing that in the background. However, when there are additions and deletions maintenance sometimes falls behind. This leads some experts to describe For Each are unreliable. I think that is too sweeping a statement. However, it may be good advice to consider whether the collection you are accessing is itself stable within the context of your code before you rely on the advantages gained from that stabiity.
For each is run for all elements in array there is no limitations
In for loop we have to do the limitations if we want to check the limitation condition we can check with for loop and we can do the limitations for some range,not all elements.
There is no limitation all the elements want to do the loop we can use foreach otherwise we move with for loop.