Stop using the traditional “for-loop” and start using forEach(). Although for-loop does have its perks, but forEach() is what’s in and since I’m a person who likes to follow the latest trends, I lean more towards forEach() rather the for loop. So, how does forEach() method work in JavaScript … let’s see some examples:
Let say you have a list of items which you want to loop through individually. Let’s see the traditional for loop method first:
let age = [34, 55, 24, 60, 13, 44]
for( let x = 0; x < age.length ; x++){
console.log( age[x] );
}
// OUTPUT = 34
// 55
// 24
// 60
// 13
// 44
Now let’s have a look at forEach() loop in javascript.
let age = [34, 55, 24, 60, 13, 44]
age.forEach( (item)=> {
console.log(item);
});
// OUTPUT = 34
// 55
// 24
// 60
// 13
// 44
In line 3, individual element of the list gets passed into the variable ‘item’ which is then printed on the console.
You could also loop though 2 different lists and have them interact, so as to have a nested loop like so:
let age = [34, 55, 24, 60, 13, 44]
let age2 = [33, 44, 55, 66, 77]
age.forEach( (i)=> {
age2.forEach( (j)=> {
if(i == j) { console.log(i); }
});
});
// OUTPUT = 55
// 44
In the above example, the values of the second array “age2” is matched with the values of the 1st array. And if they match, then that value is being printed on the console. Simple right!
Although we could use includes() method for the example above, but we’re focusing on forEach() method in this article, so I won’t go into that.
Alright so that’s the essence of what forEach() in javascript can do. There is 1 more thing that it does. It’s that it tracks the index of the element of the list too. Here let me show you:
let age = [34, 55, 24, 60, 13, 44]
age.forEach( (item, index)=> {
console.log( “At index: ” + index + “, the age is: ” + item );
});
// OUTPUT = "At index: 0, the age is: 34"
//"At index: 1, the age is: 55"
//"At index: 2, the age is: 24"
//"At index: 3, the age is: 60"
//"At index: 4, the age is: 13"
//"At index: 5, the age is: 44"
And that’s a wrap!
Hopefully I’ve managed to help you understand how exactly forEach() in JavaScript works, and of course, it also works exactly the same for TypeScript as well. Please like and share if this helped you. Have a great one.
Recent Posts
- Zonder Cruks Online Casino Veiligheid en beveiliging van spelers.1175
- Fortune Favors the Bold – Can You Lead Your Clucky Companion Down the Chicken Road & Cash Out Before
- Verfrissend Gokken met nieuwe online casino zonder cruks – Een Nieuwe Ervaring
- Elevate Your Play Find the best australian online casino Experiences with Lucrative Bonuses & Secure
- Lopebet’s Multi-Bet Options: How and When to Use Them
Recent Comments
Archives
- May 2026
- October 2025
- August 2025
- June 2025
- October 2024
- September 2024
- August 2024
- June 2023
- May 2023
- March 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
Categories
- Angular
- AWS
- Backend Development
- Big Data
- Casino
- Cloud
- Database
- Deployment
- DevOps
- Docker
- Frontend Development
- GitHub
- Google Cloud Platform
- Installations
- Java
- JavaScript
- Linux
- MySQL
- Networking
- News
- NodeJS
- Online casino
- Operating System
- Post
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Zonder Cruks Online Casino Veiligheid en beveiliging
- 18 May, 2026
- 5 min read
Fortune Favors the Bold – Can You
- 15 May, 2026
- 6 min read
Verfrissend Gokken met nieuwe online casino zonder
- 14 May, 2026
- 6 min read