Array.prototype.forEach
const number = [1, 2, 3, 4, 5];
let result = 0;
number.forEach((num) => {
result += num;
})
console.log(result);
// => 15
Array.prototype.map
const number = [1, 2, 3, 4, 5];
const result = number.map((num) => {
return num + 2
});
console.log(result);
// => [3,4,5,6,7]
Array.prototype.reduce
const number = [1, 2, 3, 4, 5];
const result = number.reduce((sum, num) => {
return sum + num;
}, 0);
console.log(result);
// => 15
Array.prototype.some
const number = [1, 2, 3, 4, 5];
const result = number.some((num) => {
return num == 3;
});
console.log(result);
// => True
Array.prototype.every
const number = [1, 2, 3, 4, 5];
const result = number.every((num) => {
return num == 3;
});
console.log(result);
// => False
Array.prototype.filter
const number = [1, 2, 3, 4, 5];
const result = number.filter((num) => {
return num % 2 == 0;
});
console.log(result);
// => [2, 4]
Array.prototype.find
const number = [1, 2, 3, 4, 5];
const result = number.find((num) => {
return num % 2 == 0;
});
console.log(result);
// => 2