🔍 JavaScript Array Methods Explained In Depth

1. map()

Definition: Creates a new array by applying a function to each element of the original array.

When to use: When you want to transform each item in an array into something else without modifying the original array.

Parameters:


const numbers = [1, 2, 3, 4];
const doubled = numbers.map((num, idx) => {
  console.log(`Index: ${idx}, Original: ${num}`);
  return num * 2;
});
console.log(doubled); // [2, 4, 6, 8]
    

Explanation: The map method calls the function on each element, doubling it here. The original numbers array remains unchanged.

2. filter()

Definition: Creates a new array containing only elements that satisfy a condition.

When to use: When you want to keep only certain elements based on a test.

Parameters:


const ages = [12, 18, 25, 15, 30];
const adults = ages.filter(age => age >= 18);
console.log(adults); // [18, 25, 30]
    

Explanation: Only ages 18 or above are kept. Others are filtered out.

3. reduce()

Reduce in Detail

Definition: Applies a function against an accumulator and each element to reduce the array to a single value.

When to use: When you want to compute a single result from an array (sum, product, flatten, etc.).

Parameters:


const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => {
  console.log(`acc: ${acc}, curr: ${curr}`);
  return acc + curr;
}, 0);
console.log(sum); // 10
    

Explanation: Starts from 0, adds each number to accumulator to get sum.

4. find()

Definition: Returns the first element that satisfies the testing function.

When to use: When you want to find one item matching criteria.

Parameters:


const users = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
  { id: 3, name: "Mark" }
];

const user = users.find(user => user.id === 2);
console.log(user); // { id: 2, name: "Jane" }
    

Explanation: Returns first object where id is 2.

5. some() & every()

Definition:

When to use: To check conditions on elements collectively.


const nums = [10, 20, 30];

// some: is there any number > 25?
console.log(nums.some(n => n > 25)); // true

// every: are all numbers > 5?
console.log(nums.every(n => n > 5)); // true
    

6. flatMap()

Definition: Maps each element then flattens the result by one level.

When to use: When mapping returns arrays and you want a single flattened array.


const sentences = ["Hello world", "How are you?"];
const words = sentences.flatMap(sentence => sentence.split(" "));
console.log(words); 
// ["Hello", "world", "How", "are", "you?"]
    

7. forEach()

Definition: Executes a provided function once for each array element.

Note: Unlike map, it does not return a new array.

When to use: When you want to perform side effects like logging or modifying external variables.


const nums = [1, 2, 3];
nums.forEach((num, idx) => {
  console.log(`Index ${idx}: ${num}`);
});
    

Parameters in Callbacks

Most array methods accept a callback function with up to three parameters:

Use parameters as needed; often only currentValue is necessary.

📌 Summary