Exambodh - Practice Aptitude, Reasoning & GK Questions

Back to Articles
JavascriptJavascript

Javascript Array Methods Reduce Filter Includes Find findindex Sort

JavascriptΒ·

Share Article

Share this article on social platforms or copy the direct link.

JavaScript Tips

JavaScript Array Methods: reduce(), filter(), includes(), find(), findIndex(), and sort() Explained with Examples

JavaScript arrays come with many built-in methods that make data handling easy and efficient. Instead of writing long loops again and again, developers can use array methods to filter, search, sort, and combine data quickly.

In this article, we will understand six very important JavaScript array methods: reduce(), filter(), includes(), find(), findIndex(), and sort(). These methods are commonly used in real projects, coding interviews, React applications, and API data handling.

β€œMastering JavaScript array methods like reduce(), filter(), and find() helps developers write cleaner, shorter, and more efficient code.”

Expert Insight

Modern JavaScript development depends heavily on array methods. Developers who understand methods like reduce(), filter(), find(), and sort() can solve real-world problems more effectively than by relying only on traditional loops.

Why These Methods Are Important

If you work with user lists, product data, marksheets, dashboards, APIs, or search results, then these array methods will be used frequently in your code.

They help you reduce code complexity and improve readability. That is why these methods are also commonly asked in JavaScript and React interviews.

1. reduce()

The reduce() method reduces an array to a single value by applying a callback function on each element.

What it does

Combines all array elements into one final value.

Use case

Useful when you want totals, sums, summary values, or concatenation.

let emotions = ["😊", "😱", "😐"];

let allEmotions = emotions.reduce(
  (acc, emotion) => acc + emotion,
  ""
);

console.log(allEmotions);
// "😊😱😐"

Here, reduce() combines all array values into a single string.

Practical Example

let numbers = [10, 20, 30];

let total = numbers.reduce((sum, num) => sum + num, 0);

console.log(total);
// 60

2. filter()

The filter() method returns a new array containing only those elements that satisfy a given condition.

What it does

Filters array items based on a condition.

Use case

Useful when you want only specific items from a list.

let emotions = ["😊", "😱", "😑", "😐"];

let happyEmotions = emotions.filter(
  emotion => emotion === "😊" || emotion === "😐"
);

console.log(happyEmotions);
// ["😊", "😐"]

Here, filter() returns only the matching elements.

Practical Example

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

let result = numbers.filter(num => num > 10);

console.log(result);
// [15, 20]

3. includes()

The includes() method checks whether a given value exists in an array. It returns true or false.

What it does

Checks if an array contains a particular value.

Use case

Useful when you want to verify the existence of an item.

let emotions = ["😊", "😱", "😑"];

console.log(emotions.includes("😊"));
// true

console.log(emotions.includes("😐"));
// false

Here, includes() checks whether the given element is present in the array.

Practical Example

let roles = ["Admin", "Editor", "User"];

console.log(roles.includes("Admin"));
// true

4. find()

The find() method returns the first element in an array that satisfies a condition.

What it does

Returns the first matching item from an array.

Use case

Useful when you are searching for one specific item.

let emotions = ["😊", "😱", "😐"];

let firstHappy = emotions.find(
  emotion => emotion === "😊"
);

console.log(firstHappy);
// "😊"

If a matching value is found, find() returns that value. Otherwise, it returns undefined.

Practical Example

let users = [
  { id: 1, name: "Rahul" },
  { id: 2, name: "Aman" }
];

let user = users.find(u => u.id === 2);

console.log(user);
// { id: 2, name: "Aman" }

5. findIndex()

The findIndex() method returns the index position of the first element that matches a condition.

What it does

Returns the position of the first matching element.

Use case

Useful when you want to know where an item exists in the array.

let emotions = ["😊", "😱", "😐"];

let index = emotions.findIndex(
  emotion => emotion === "😐"
);

console.log(index);
// 2

Here, the matching element "😐" is found at index 2.

Practical Example

let numbers = [10, 20, 30];

let index = numbers.findIndex(n => n === 20);

console.log(index);
// 1

6. sort()

The sort() method sorts the elements of an array. It changes the original array directly.

What it does

Sorts array elements in place.

Use case

Useful when you need data in sorted order.

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

numbers.sort();

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

Sorting Strings

let characters = ["b", "d", "a", "c"];

characters.sort();

console.log(characters);
// ["a", "b", "c", "d"]

Important Note for Numbers

For numeric sorting, it is better to use a compare function.

let numbers = [10, 2, 30, 4];

numbers.sort((a, b) => a - b);

console.log(numbers);
// [2, 4, 10, 30]

Quick Summary

  • reduce() reduces an array to a single value.
  • filter() returns items matching a condition.
  • includes() checks if a value exists in the array.
  • find() returns the first matching element.
  • findIndex() returns the index of the first matching element.
  • sort() sorts elements of an array.

Key Takeaway

If you want to become stronger in JavaScript, do not just memorise syntax. Understand where to use each method: filter() for selection, find() for searching, includes() for checking, reduce() for summarising, and sort() for ordering data.

More In Javascript

Explore more Javascript articles

Read more articles from the same category or open the full category archive directly.