. For and Foreach loop in Javascript Skip to main content

For and Foreach loop in Javascript

  • Don't use for-in unless you use it with safeguards or are at least aware of why it might bite you.
  • Your best bets are usually
    • a for-of loop (ES2015+ only),
    • Array#forEach (spec | MDN) (or its relatives some and such) (ES5+ only),
    • a simple old-fashioned for loop,
    • or for-in with safeguards.
But there's lots more to explore, read on...

JavaScript has powerful semantics for looping through arrays and array-like objects. I've split the answer into two parts: Options for genuine arrays, and options for things that are just array-like, such as the arguments object, other iterable objects (ES2015+), DOM collections, and so on.


For Actual Arrays

You have three options in ECMAScript 5 ("ES5"), the version most broadly supported at the moment, and will soon have two more in ECMAScript 2015 ("ES2015", "ES6"), the latest version of JavaScript that vendors are working on supporting:
  1. Use forEach and related (ES5+)
  2. Use a simple for loop
  3. Use for-in correctly
  4. Use for-of (use an iterator implicitly) (ES2015+)
  5. Use an iterator explicitly (ES2015+)

1. Use forEach and related

If you're using an environment that supports the Array features of ES5 (directly or using a shim), you can use the new forEach (spec | MDN):
 var a = ["a", "b", "c"];  
 a.forEach(function(entry) {  
   console.log(entry);  
 });  


forEach accepts an iterator function and, optionally, a value to use as this when calling that iterator function (not used above). The iterator function is called for each entry in the array, in order, skipping non-existent entries in sparse arrays. Although I only used one argument above, the iterator function is called with three: The value of each entry, the index of that entry, and a reference to the array you're iterating over (in case your function doesn't already have it handy).

Unless you're supporting obsolete browsers like IE8 (which NetApps shows at just over 4% market share as of this writing in September 2016), you can happily use forEach in a general-purpose web page without a shim. If you do need to support obsolete browsers, shimming/polyfilling forEach is easily done (search for "es5 shim" for several options).

forEach has the benefit that you don't have to declare indexing and value variables in the containing scope, as they're supplied as arguments to the iteration function, and so nicely scoped to just that iteration.

Additionally, forEach is the "loop through them all" function, but ES5 defined several other useful "work your way through the array and do things" functions, including:
  • every (stops looping the first time the iterator returns false or something falsey)
  • some (stops looping the first time the iterator returns true or something truthy)
  • filter (creates a new array including elements where the filter function returns true and omitting the ones where it returns false)
  • map (creates a new array from the values returned by the iterator function)
  • reduce (builds up a value by repeated calling the iterator, passing in previous values; see the spec for the details; useful for summing the contents of an array and many other things)
  • reduceRight (like reduce, but works in descending rather than ascending order)

2. Use a simple for loop

Sometimes the old ways are the best:

 var index;  
 var a = ["a", "b", "c"];  
 for (index = 0; index < a.length; ++index) {  
   console.log(a[index]);  
 }  
If the length of the array won't change during the loop, and it's in performance-sensitive code (unlikely), a slightly more complicated version grabbing the length up front might be a tiny bit faster:
 var index, len;  
 var a = ["a", "b", "c"];  
 for (index = 0, len = a.length; index < len; ++index) {  
   console.log(a[index]);  
 }  

And/or counting backward:
 var index;  
 var a = ["a", "b", "c"];  
 for (index = a.length - 1; index >= 0; --index) {  
   console.log(a[index]);  
 }  

3. Use for-in correctly

You'll get people telling you to use for-in, but that's not what for-in is for. for-in loops through the enumerable properties of an object, not the indexes of an array. The order is not guaranteed, not even in ES2015 (ES6). ES2015 does define an order to object properties (via [[OwnPropertyKeys]], [[Enumerate]], and things that use them like Object.getOwnPropertyKeys), but it does not define that for-in will follow that order
Still, it can be useful, particularly for sparse arrays, if you use appropriate safeguards:


 // `a` is a sparse array  
 var key;  
 var a = [];  
 a[0] = "a";  
 a[10] = "b";  
 a[10000] = "c";  
 for (key in a) {  
   if (a.hasOwnProperty(key) &&    // These are explained  
     /^0$|^[1-9]\d*$/.test(key) &&  // and then hidden  
     key <= 4294967294        // away below  
     ) {  
     console.log(a[key]);  
   }  
 }  

 Or if you're interested in just a "good enough for most cases" test, you could use this, but while it's close, it's not quite correct:
 for (key in a) {  
   // "Good enough" for most cases  
   if (String(parseInt(key, 10)) === key && a.hasOwnProperty(key)) {  
     console.log(a[key]);  
   }  
 }  

4. Use for-of (use an iterator implicitly) (ES2015+)

ES2015 adds iterators to JavaScript. The easiest way to use iterators is the new for-of statement. It looks like this:
 var val;  
 var a = ["a", "b", "c"];  
 for (val of a) {  
   console.log(val);  
 }  

Output:
 a  
 b  
 c  

5. Use an iterator explicitly (ES2015+)

Sometimes, you might want to use an iterator explicitly. You can do that, too, although it's a lot clunkier than for-of. It looks like this:
 var a = ["a", "b", "c"];  
 var it = a.values();  
 var entry;  
 while (!(entry = it.next()).done) {  
   console.log(entry.value);  
 }  


Full Source : Stackoverflow

Comments

Popular posts from this blog

Day 9 - Challenge 1 - Product of Array Except Self

Solving the "Product of Array Except Self" Problem in JavaScript Are you ready to dive into a common coding challenge that not only tests your programming skills but also sharpens your problem-solving mindset? If you're up for the challenge, let's tackle the "Product of Array Except Self" problem together using JavaScript. This problem requires us to return an array where each element at index i is the product of all the elements in the original array except the one at index i . Understanding the Problem:   Imagine you're given an array of integers, let's call it nums . Your task is to create a new array where the value at index i in this new array is the product of all the elements in nums , except the one at index i . In other words, you're calculating the product of all the elements to the left of nums[i] and the product of all the elements to the right of nums[i] , and then multiplying these two products to get the final value at index i ...

Day 9 - Challenge 2 - Reverse Linked List

Reversing a Singly Linked List in JavaScript: An In-Place Approach Introduction:   Singly linked lists are fundamental data structures in computer science that consist of a sequence of nodes, each containing data and a reference to the next node in the list. Reversing a singly linked list is a classic problem that challenges programmers to manipulate pointers effectively to achieve the desired outcome. In this blog post, we'll explore the problem of reversing a singly linked list using an in-place approach and provide a step-by-step solution in JavaScript. Problem Statement:   Given the head of a singly linked list, our task is to reverse the list in-place and return its new head. In other words, we need to modify the pointers of the nodes in such a way that the direction of the linked list is reversed. Solution Approach:   To solve this problem, we will iterate through the linked list while maintaining three pointers: previous , current , and next . The previous pointer...

Day 8 - Challenge 2 - Move Zeroes to the End

Moving Zeroes to the End: A JavaScript Solution Introduction:   When working with arrays, there are often times when we need to manipulate their elements to achieve a specific goal. One common problem is moving all zeroes to the end of an array while keeping the order of non-zero elements unchanged. In this blog post, we will explore an elegant solution to this problem using JavaScript. We'll discuss the problem statement, the approach we'll take, and provide a step-by-step guide to implementing the solution. The Problem:   Given an array of integers, the task is to move all zeroes to the end of the array while maintaining the relative order of the non-zero elements. This means that after rearranging the array, all the zeroes should be at the end, and the order of the non-zero elements should remain the same. The Approach:   To solve this problem, we can utilize a two-pointer approach. We'll maintain two pointers, one for iterating through the array and another for keepin...