Member-only story

10 Must-Know JavaScript Array Methods

habtesoft
3 min readNov 28, 2024

--

JavaScript arrays are incredibly versatile, and mastering their methods can make your code cleaner, faster, and more readable. This article highlights 10 essential array methods every developer should know, complete with examples to help you understand their power and applications.

Not a Medium member? Read this article here

1. map() – Transforming Data

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

Example:

const numbers = [1, 2, 3, 4];
const squared = numbers.map(num => num ** 2);
console.log(squared); // [1, 4, 9, 16]

2. filter() – Selecting Elements

The filter() method returns a new array with elements that satisfy a given condition.

Example:

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

3. reduce() – Summarizing Data

The reduce() method applies a function to each element to reduce the array to a single value.

Example:

const prices = [10, 20, 30];
const total = prices.reduce((sum, price) => sum + price, 0);
console.log(total); // 60

4. forEach() – Iterating Over Elements

The forEach() method executes a callback function for each element in the array.

Example:

const fruits = ['apple', 'banana', 'cherry'];
fruits.forEach(fruit => console.log(fruit));

5. find() – Locating the First Match

The find() method returns the first element that satisfies the provided condition.

Example:

const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
const user = users.find(u => u.id === 2);
console.log(user); // { id: 2 }

6. findIndex() – Locating the Index of a Match

--

--

habtesoft
habtesoft

Written by habtesoft

Passionate JavaScript developer with a focus on backend technologies. Always eager to connect and learn. Let’s talk, https://buymeacoffee.com/habtesoftat

No responses yet

Write a response