Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
JavaScript provides a multitude of ways for implementing loops to traverse through an array. Today we are going to discuss one particular loop that has quickly turned into a favourite amongst developers; the forEach loop in JavaScript. Below are the topics that we will be looking into:
Loop is the term for the process of implementing an iterator i.e. something that is repetitively done. So if you were to count from 1 to 10, you would implement a loop that loops ten times, and increases a counting value by 1.
Arrays are simply put, a collection of similar objects. It’s generally great for maintaining a list of things, for example, student information, which could be stored as a student object in an array. A great way to traverse an array has been a for loop, and that is exactly what the forEach loop improves upon. Let’s learn more about the forEach loop.
student_names = [ 'Rob', 'Van', 'Dam'] studentNames.forEach((student) => { //You can perform your desired function out here print(student) }
The above snip is the syntax of a forEach loop in JavaScript. Let’s take a closer look at how this is all executed. We firstly declare an array of student names and name it appropriately so. Then we invoke the forEach function with the dot(.) operator. The data that is returned by the function is stored in student. The data is returned by a callback function. In this example, we simply print the names of the student which would give the output ‘Rob Van Dam’
The parameters are passed in the callback function, they are-
undefined. Always.
Filter, Map return an array, and forEach returns undefined. This is the main difference between these loops.
Now that the basics are done, let’s go over a few rules to be kept in mind when using forEach.
These are the rules that are always to be kept in mind when using the forEach loop.
Now that you know about the forEach loop, check out the Web Development Certification Training by Edureka. Web Development Certification Training will help you learn how to create impressive websites using HTML5, CSS3, Twitter Bootstrap 3, jQuery and Google APIs and deploy it to Amazon Simple Storage Service(S3).
Got a question for us? Please mention it in the comments section of “forEach in JavaScript” and we will get back to you.
Course Name | Date | Details |
---|---|---|
Full Stack Web Development Course | Class Starts on 2nd November,2024 2nd November SAT&SUN (Weekend Batch) | View Details |
edureka.co