Looping Through Arrays (for Loops, forEach, for...of)
The for of loop in JavaScript allows you to iterate over iterable objects such as arrays, strings, and HTML collections with ease.
Lets Go!

Looping Through Arrays (for Loops, forEach, for...of)
Lesson 13
Understand how to loop through arrays using different techniques, including for, forEach, and for...of.
Get Started 🍁Introduction to JavaScript's For Of Loop
Welcome to the course on Introduction to JavaScript's For Of Loop! In this course, we will delve into the functionality and usage of the for of loop within JavaScript.
The for of loop allows you to iterate over iterable objects such as arrays, strings, sets, and HTML collections with ease. Instead of traditional looping methods, the for of loop simplifies the process by directly accessing the elements.
Have you ever wondered how to efficiently loop over different data structures in JavaScript? If so, this course is perfect for you!
No prior experience with JavaScript's for of loop is required to enroll in this course. We will guide you through the basics and help you master this convenient looping mechanism.
Join us as we explore examples of using the for of loop with arrays, strings, and HTML collections. Get ready to enhance your JavaScript skills and streamline your coding practices!
Let's embark on this exciting journey together and unlock the potential of JavaScript's for of loop. Start your learning adventure now!
Main Concepts of JavaScript's 'for of' Loop
-
What is a 'for of' loop?
- The 'for of' loop in JavaScript allows you to iterate over iterable objects like arrays, strings, sets, and HTMLCollections.
-
Traditional way of iteration
- Before the 'for of' loop, you would iterate over an array using a 'for' loop and index referencing.
- For example:
for (var i = 0; i < a.length; i++) { console.log(a[i]); }
-
Simplified iteration with 'for of' loop
- With the 'for of' loop, iterating over an array becomes much simpler.
- Example:
for (const element of a) { console.log(element); }
-
Using 'const' in 'for of' loop
- Use
const
when iterating with 'for of' loop if you're not reassigning the variable. - Use
let
if you need to reassign the variable inside the loop.
- Use
-
Iterating over strings with 'for of'
- 'for of' loop can also be used to iterate over characters in a string.
- Example:
for (const char of name) { console.log(char); }
-
Working with HTMLCollections
- You can iterate over HTMLCollections like a list of elements.
- Example:
const children = document.querySelectorAll('#myList li'); for (const listItem of children) { console.log(listItem); }
-
Conclusion
- The 'for of' loop simplifies iteration over iterable objects in JavaScript, making code more readable and efficient.
Practical Applications of For...Of Loop in JavaScript
The for...of
loop in JavaScript allows you to easily iterate over iterable objects like arrays, strings, sets, and HTML collections. Below are step-by-step guides for practical applications:
1. Iterate over an Array:
const aDouble = [10, 20, 'Dominick']; for (const element of aDouble) { console.log(element); }
- Define an array with elements you want to iterate over.
- Use the
for...of
loop to loop through each element in the array. - Log each element to the console.
2. Iterate over a String:
const name = 'Dominick'; for (const letter of name) { console.log(letter); }
- Define a string you want to iterate over.
- Use the
for...of
loop to loop through each character in the string. - Log each character to the console.
3. Iterate over an HTML Collection:
const children = document.querySelectorAll('#my lists li'); for (const listItem of children) { console.log(listItem); }
- Define an HTML collection (in this case, list items with an ID of 'my lists').
- Use the
for...of
loop to loop through each item in the HTML collection. - Log each item to the console.
Try It Out!
- Create your own arrays, strings, or HTML collections to practice using the
for...of
loop in JavaScript! - Modify the examples to suit your needs and see the loop in action. Happy coding!
Test your Knowledge
Which loop is best suited for directly accessing array elements?
Which loop is best suited for directly accessing array elements?
Advanced Insights into Looping in JavaScript
In this advanced section, we will delve deeper into the for of
loop in JavaScript and explore its applications with different iterable objects beyond arrays.
Deeper Understanding:
The for of
loop allows iteration over iterable objects such as arrays, strings, sets, and HTML collections. It simplifies the process of looping by directly providing the values of each element in the object without the need for manual indexing.
Expert Tips:
- When using
for of
with arrays, consider usingconst
for the loop variable if you are not planning to reassign it. - For iterating over strings, use the same syntax as with arrays, treating each character as an element in the loop.
- When working with HTML collections, leverage the
document.querySelectorAll()
method to target specific elements for iteration.
Expert Recommendation:
Experiment with various iterable objects and practice using the for of
loop to enhance your understanding of iteration in JavaScript.
Curiosity Question:
How can you optimize the use of the for of
loop when dealing with deeply nested data structures in JavaScript?
Additional Resources for JavaScript Loops
Explore these resources to enhance your understanding of JavaScript loops and how to effectively use the for...of loop to iterate over iterable objects. Happy coding!
Practice
Task: Create an array of numbers and loop through it using a for loop to log each element.
Task: Use the forEach method to calculate the sum of all elements.
Task: Use the for...of loop to log each element in uppercase (if the array contains strings).