0% found this document useful (0 votes)
33 views

Arrow Function Array Mehod

Uploaded by

heseta8635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Arrow Function Array Mehod

Uploaded by

heseta8635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Arrow Function

Arrow Functions vs Normal Functions in JavaScript


1. Syntax:
• Arrow Function: Provides a shorter, cleaner syntax.
const add = (a, b) => a + b;
• Normal Function: Longer, more traditional syntax.
function add(a, b) {
return a + b;
}

2. Return Statements:
• Arrow Function: Implicit return if there's a single expression (no need for
return keyword).
const square = x => x * x;
• Normal Function: Requires an explicit return statement.
function square(x) {
return x * x;
}

Array Iteration Method

1. forEach()
The forEach() method allows you to run a function once for each item in an array. It doesn't
return anything and is mostly used for running some code on each element.
• Use Case: Running a piece of code for every item in the array.
Example:
const numbers = [1, 2, 3];
numbers.forEach(num => {
console.log(num * 2);
});
// Output: 2 4 6
In this example, each number in the array is multiplied by 2 and
printed. But, forEach doesn’t give back a new array.

2. map()
The map() method creates a new array by applying a function to each item in the original
array. It transforms each item into something else and returns a new array.
• Use Case: Transforming each element in an array and returning a new array.
Example:
const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
// Output: [2, 4, 6]
Here, each number is doubled, and a new array [2, 4, 6] is
returned.

3. filter()
The filter() method creates a new array with items that pass a condition (true/false test). If
an item doesn’t pass the test, it won’t be included in the new array.
• Use Case: Picking only specific elements from an array.
Example:
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers);
// Output: [2, 4]
This example filters out only the even numbers (2 and 4) from
the array.

4. reduce()
The reduce() method goes through each item in the array and combines them into a single
value. It’s commonly used to add up numbers or combine data into one result.
• Use Case: Combining all items in an array into one value (like sum, product, etc.).
Example:
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum);
// Output: 10
Here, reduce adds all numbers together, and the result is 10.

Summary:
• forEach(): Runs a function on each array item, but doesn’t return anything.
• map(): Transforms each item in an array and returns a new array.
• filter(): Returns a new array with items that pass a certain test.
• reduce(): Combines array items into a single result, like a sum.

You might also like