Sanjoy Saha

Dec 19, 2024 • 2 min read • 

JavaScript Array Functions

JavaScript Array Functions

Introduction

Arrays are commonly used data structures in JavaScript. JavaScript provides a range of powerful array functions. In this blog, I had mentioned most used array methods, their use cases and practical examples to help you master them.


Commonly Used JavaScript Array Functions

1. map()

It transforms every element in an array and returns a new array.

Example:

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

2. filter()

Filters the array based on a condition and returns a new array with elements that pass the condition.

Example:

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

3. reduce()

Reduces the array to a single value by applying a function to each element.

Example:

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

4. forEach()

Executes a function for each element in the array.

Example:

const numbers = [1, 2, 3];
numbers.forEach(num => console.log(num * 2));
// Output: 2, 4, 6

5. find()

Returns the first element that satisfies a condition.

Example:

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

6. findIndex()

Returns the index of the first element that satisfies a condition.

Example:

const numbers = [1, 2, 3, 4];
const index = numbers.findIndex(num => num > 2);
console.log(index); // 2

7. some()

Checks if at least one element satisfies a condition.

Example:

const numbers = [1, 2, 3];
const hasNegative = numbers.some(num => num < 0);
console.log(hasNegative); // false

8. every()

Checks if all elements satisfy a condition.

Example:

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

9. sort()

Sorts the elements of an array in place.

Example:

const numbers = [4, 2, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers); // [1, 2, 3, 4]

10. splice()

Adds or removes elements from an array.

Example:

const numbers = [1, 2, 3, 4];
numbers.splice(1, 2, 99, 100);
console.log(numbers); // [1, 99, 100, 4]

11. slice()

Extracts a portion of an array and returns a new array.

Example:

const numbers = [1, 2, 3, 4];
const subArray = numbers.slice(1, 3);
console.log(subArray); // [2, 3]

12. concat()

Merges two or more arrays into a new array.

Example:

const arr1 = [1, 2];
const arr2 = [3, 4];
const merged = arr1.concat(arr2);
console.log(merged); // [1, 2, 3, 4]

Conclusion

Mastering JavaScript array functions can enhance your coding skills and allow you to write cleaner, efficient and more readable code. Start with the basics and gradually you can solve coding problems effectively.

Join Sanjoy on Peerlist!

Join amazing folks like Sanjoy and thousands of other people in tech.

Create Profile

Join with Sanjoy’s personal invite link.

0

7

0