JavaScript - map,filter,reduce,find ,some,every,sort functions

forEach(), map(), filter(), reduce(), find(), findIndex(), some(), every(), sort(), and reverse() in JavaScript with simple explanations and examples.


1. forEach()

The forEach() method executes a function for each element in an array.
It does not return a new array — it’s mainly used for looping.

Syntax:

array.forEach(function(element, index, array) {
   // code here
});

Example:

let numbers = [1, 2, 3, 4];

numbers.forEach((num, index) => {
    console.log(`Index: ${index}, Value: ${num}`);
});

Output:

Index: 0, Value: 1
Index: 1, Value: 2
Index: 2, Value: 3
Index: 3, Value: 4

2. map()

The map() method creates a new array by applying a function to each element.

Syntax:

let newArray = array.map(function(element, index, array) {
   return newValue;
});

Example:

let numbers = [1, 2, 3, 4];

let doubled = numbers.map(num => num * 2);
console.log(doubled);  // [2, 4, 6, 8]

Key Point:

  • forEach()does not return a new array.

  • map()always returns a new array.


3. filter()

The filter() method creates a new array with elements that pass a condition.

Syntax:

let newArray = array.filter(function(element, index, array) {
   return condition;
});

Example:

let numbers = [5, 10, 15, 20, 25];

let greaterThan15 = numbers.filter(num => num > 15);
console.log(greaterThan15);  // [20, 25]

4. reduce()

The reduce() method reduces an array to a single value by applying a function accumulatively.

Syntax:

array.reduce(function(accumulator, currentValue, index, array) {
   return result;
}, initialValue);
  • accumulator → stores the result.

  • currentValue → current element being processed.

  • initialValue (optional) → starting value.

Example 1: Sum of Numbers

let numbers = [1, 2, 3, 4];

let sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum);  // 10

Example 2: Find Maximum Number

let nums = [10, 25, 5, 40];

let max = nums.reduce((a, b) => (a > b ? a : b));
console.log(max);  // 40

5. find() and findIndex()

(a) find()

  • Returns the first element that matches a condition.

  • Returns undefined if no match is found.

let numbers = [10, 20, 30, 40];

let found = numbers.find(num => num > 25);
console.log(found);  // 30

(b) findIndex()

  • Returns the index of the first matching element.

  • Returns -1 if not found.

let numbers = [10, 20, 30, 40];

let index = numbers.findIndex(num => num > 25);
console.log(index);  // 2

6. some() and every()

(a) some()

  • Checks if at least one element passes the condition.

  • Returns true or false.

let numbers = [1, 2, 3, 4];

console.log(numbers.some(num => num > 3));  // true
console.log(numbers.some(num => num > 10)); // false

(b) every()

  • Checks if all elements pass the condition.

  • Returns true or false.

let numbers = [2, 4, 6];

console.log(numbers.every(num => num % 2 === 0));  // true
console.log(numbers.every(num => num > 3));        // false

7. sort()

The sort() method sorts an array.
By default, it sorts alphabetically (as strings).

Example 1: Sorting Strings

let fruits = ["Banana", "Apple", "Mango"];
fruits.sort();
console.log(fruits);  // ["Apple", "Banana", "Mango"]

Example 2: Sorting Numbers (Correct Way)

let numbers = [40, 100, 5, 25];

numbers.sort((a, b) => a - b);  // Ascending
console.log(numbers);  // [5, 25, 40, 100]

numbers.sort((a, b) => b - a);  // Descending
console.log(numbers);  // [100, 40, 25, 5]

8. reverse()

The reverse() method reverses the order of elements in place.

Example:

let numbers = [1, 2, 3, 4];

numbers.reverse();
console.log(numbers);  // [4, 3, 2, 1]

Quick Comparison Table

Method Purpose Returns Modifies Original?
forEach() Loops through elements undefined ❌ No
map() Transforms elements New array ❌ No
filter() Keeps elements that match condition New array ❌ No
reduce() Reduces array to a single value Single value ❌ No
find() Finds first matching element Element or undefined ❌ No
findIndex() Finds index of first matching element Number ❌ No
some() Checks if any element matches true / false ❌ No
every() Checks if all elements match true / false ❌ No
sort() Sorts array Sorted array ✅ Yes
reverse() Reverses array order Reversed array ✅ Yes