Hi all. I already know how to use the .filter()
and .forEach()
method. And I was wondering about difference between .filter()
and .forEach()
method.
For example with .filter()
method:
let arrayOfNumbers = [
3
7
2
9
4
6
];
function getEvenNumbers(nums) { // Function setup
for (var num of nums) { // for...of loop
if (num % 2 === 0) { // if statement
console.log(num); // statement to run
}
}
};
let evenNumbers = arrayOfNumbers.filter(getEvenNumbers);
And the .forEach()
method:
let fruitList = [
'Apple',
'Banana',
'Orange'
];
function printArray(arr) {
console.log(arr);
};
fruitList.forEach(printArray);
Is there any difference between .filter()
and .forEach()
method?
Please answer as fast as possible.
Thank you!
Pummarin