← Back to snippets

Useful JavaScript Array Methods

A collection of powerful JavaScript array methods that every developer should know

javascript
intermediate

JavaScript Array Methods Every Developer Should Know

JavaScript offers a variety of built-in array methods that can make your code more elegant and readable. Let's explore some of the most useful ones.

1. Map

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

type OptionalMethods<T> = {
  [K in keyof T as T[K] extends (...args: any[]) => any ? K : never]?: T[K];
} & {
  [K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: T[K];
};

2. Filter

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]

3. Reduce

The reduce() method executes a reducer function on each element of the array, resulting in a single output value.

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, current) => acc + current, 0);
console.log(sum); // 15

4. Find

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

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

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

5. Some

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // true

6. Every

The every() method tests whether all elements in the array pass the test implemented by the provided function.

const numbers = [1, 2, 3, 4, 5];
const allPositive = numbers.every(num => num > 0);
console.log(allPositive); // true

These methods can be chained together to create powerful data transformations with clean, readable code.