How to use map() Method In Javascript
A map() Method applies a function over every element and then returns the new array.
Example: In this example we are using map() method for iteration of an array.
let x = 0;
let array = [1, 2, 3, 4, 5, 6];
let square = x => Math.pow(x, 2);
square = array.map(square);
console.log(array);
console.log(square);
Output
[ 1, 2, 3, 4, 5, 6 ] [ 1, 4, 9, 16, 25, 36 ]
Iterate over an array in JavaScript
Iterating over arrays in JavaScript is a fundamental task that developers frequently perform. JavaScript provides several methods to iterate through arrays, including for, forEach(), map(), filter(), reduce(), and for…of. Each method has its specific use cases, benefits, and best practices. This guide explores these different array iteration methods, demonstrating how to effectively loop through arrays to manipulate and access their elements in JavaScript.
There are many ways to iterate through an array in JavaScript:
Table of Content
- 1. Using console.log() Method
- 2. Using for Loop
- 3. Using while loop
- 4. Using forEach() Method
- 5. Using every() Method
- 6. Using map() Method
- 7. Using filter() Method
- 8. Using reduce() Method
- 9. Using some() Method
- 10. Using entries() Method