Written by Sümeyye Sever (notes I took while creating web development projects)
The main difference between map and forEach is that map returns a new array with the results of the function applied to each element, while forEach does not return anything. Both map and forEach can modify the original array if the callback function modifies its elements.
//.map() function
const numbers = [1, 4, 9];
const roots = numbers.map((num)=> Math.sqrt(num));
//roots is now [1, 2, 3]
//numbers is still [1, 4, 9]
//.forEach() function
const numbers = [1, 4, 9];
const roots = [];
numbers.forEach((num) => {
roots.push(Math.sqrt(num));
});
//roots is now [1, 2, 3]
//numbers is still [1, 4, 9]
//.filter() function
const words = ["python", "javascript", "java", "typescript", "c"];
const filteredWords = words.filter((word) => word.length > 6 );
//filteredWords is now ["javascript", "typescript"]