

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using iterator functions in Javascript
Other than Explicit iteration, Javascript provides a variety of iteration functions that you can use to iterate over arrays. Let's look at some of these functions −
ForEach Function
This function executes the function you pass to it for every object in the array. For example,
Example
let people = ['Harry', 'Martha', 'John', 'Sam'] people.forEach(person => console.log(person.toUpperCase()));
This will give the output −
Output
HARRY MARTHA JOHN SAM
Map Function
This function executes the function you pass to it for every object in the array and creates a new array based on what you return to it. For example,
Example
let people = ['Harry', 'Martha', 'John', 'Sam'] let upperCaseNames = people.map(person => person.toUpperCase()) console.log(upperCaseNames);
Output
This will give the output −
[ 'HARRY', 'MARTHA', 'JOHN', 'SAM' ]
Filter Function
This function executes the function you pass to it for every object in the array and creates a new array based on the values that return a truthy value. For example,
Example
let people = ['Harry', 'Martha', 'John', 'Sam'] console.log(people.filter(person => person[0] === 'H'));
This will give the output −
Output
['Harry']
There are many other such functions like reduce, every, some, etc that you can read more about on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
- Related Questions & Answers
- Iterator Functions in C#
- Iterator Functions in Python
- Iterator Functions in Java
- JavaScript Encapsulation using Anonymous Functions
- Regular functions vs Arrow functions in JavaScript?
- Arrow functions in Javascript
- Difference between regular functions and arrow functions in JavaScript
- Difference between Iterator and Spilt Iterator in Java.
- How to iterate List using Iterator in Java?
- What are functions in JavaScript?
- Explain Generator functions in JavaScript?
- Async/Await Functions in JavaScript
- Explain shorthand functions in JavaScript?
- Fat arrow functions in JavaScript
- Concise arrow functions in JavaScript