Harnessing the Power of Array forEach(): A Beginner's Guide
Introduction to forEach()
The forEach() method is an array method in JavaScript that executes a function for each element in the array. It is an easy and convenient way to iterate over an array and perform an action for each element.
Syntax of forEach()
The syntax for forEach() is simple:
array.forEach(function(element, index, array) {
// code to be executed for each element in the array
});
The first parameter of the callback function is the current element being processed, the second is the index of that element, and the third is the array itself.
Returning values with forEach()
One important thing to note is that forEach() does not return anything. It simply calls the provided function once for each element in the array. This means you cannot use forEach() to create a new array based on the original. However, you can modify the original array using the function passed to forEach().
For example, let's say we want to square each number in our original array and store the result in a new array. We cannot use forEach() for this, but we can use another method called map(). Here is an example:
let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map(function(num) {
return num ** 2;
});
console.log(squaredNumbers); // Output: [1, 4, 9, 16, 25]
In this example, we are using the map() method to create a new array called squaredNumbers. We are passing a function as an argument that takes each number in the original array and squares it using the exponentiation operator (**). The map() method then returns a new array with the squared numbers.
Real-world example
A real-world example of using forEach() is manipulating the HTML DOM. The DOM (Document Object Model) is a tree-like representation of an HTML document and you can use JavaScript to manipulate its elements.
Let's say we have a list of items on a web page and we want to add an event listener to each item that logs its text content to the console when clicked. We can do this using forEach() and the DOM API:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
let items = document.querySelectorAll('li');
items.forEach(function(item) {
item.addEventListener('click', function() {
console.log(item.textContent);
});
});
In this example, we are selecting all <li> elements using document.querySelectorAll() and then using forEach() to add an event listener to each one. The event listener logs the text content of the element to the console when clicked.
Conclusion
to summarize, the forEach() is a useful method for iterating over the elements in an array and performing a function for each one of them. It's important to note that the forEach() doesn't return anything and can't be used to create a new array. However, it can be used to modify the original array. Additionally, the forEach() is commonly used in conjunction with the DOM API to manipulate HTML elements. I hope you found this guide helpful and can apply this information to your own projects.
Subscribe to my newsletter
Read articles from Abner Kallil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Abner Kallil
Abner Kallil
I'm a software developer from Brazil