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
- Haaienjacht op de rollen de spanning van de chicken road game casino
- Překvapivé příležitosti a herní svět goldzino – pro radost a napětí
- Fast Payout Casinos Ireland.2848
- Spinsy Casino France comment jouer dans un casino en ligne moderne.2354
- Casino Yep PL Kompletny przewodnik po platformie kasyna online.85
Recent Comments
Archives
- June 2026
- May 2026
- October 2025
- October 2024
- September 2024
- August 2024
- June 2024
- January 2024
- November 2023
- October 2023
- September 2023
- August 2023
- 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
- 21
- Angular
- AWS
- Backend Development
- Big Data
- Blog
- Cloud
- crypto 20
- crypto 20.05
- crypto 21.05
- Database
- Deployment
- DevOps
- Docker
- fi
- Frontend Development
- GitHub
- Google Cloud Platform
- hu
- Installations
- it
- Java
- JavaScript
- Linux
- MySQL
- Networking
- News
- nl
- no
- NodeJS
- Online casino
- Operating System
- Post
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Haaienjacht op de rollen de spanning van
- 3 June, 2026
- 8 min read
Překvapivé příležitosti a herní svět goldzino –
- 3 June, 2026
- 7 min read
Fast Payout Casinos Ireland.2848
- 3 June, 2026
- 7 min read