DEV Community

Cover image for Most Useful JavaScript Array Methods Every Developer Should Know
bala senthil
bala senthil

Posted on

Most Useful JavaScript Array Methods Every Developer Should Know

JavaScript array methods

JavaScript arrays are one of the most powerful and commonly used data structures in web development. Whether you're working with user data, product lists, or API responses, arrays help you store and manipulate collections of data efficiently.

JavaScript provides many built-in array methods that make it easy to add, remove, transform, and iterate through data. In this article, we'll explore some of the most useful JavaScript array methods every developer should know.

1. push() – Add Element to the End

The push() method in JavaScript is used to add one or more elements to the end of an array. It modifies the original array and returns the new length of the array.

const employee = ['Ajay', 'Vijay', 'Rahul'];

employee.push('Bala');

console.log(employee);
Enter fullscreen mode Exit fullscreen mode

Output: ['Ajay', 'Vijay', 'Rahul', 'Bala']

This method is commonly used when adding new items to a list.

2. pop() – Remove Last Element

The pop() method in JavaScript is used to remove the last element from an array.

It modifies the original array and returns the removed element.

const employee = ['Ajay', 'Vijay', 'Rahul'];

employee.pop();

console.log(employee);
Enter fullscreen mode Exit fullscreen mode

Output: ['Ajay', 'Vijay']

The last element "Rahul" is removed.

3. shift() – Remove First Element

The shift() method in JavaScript is used to remove the first element from an array.

It changes the original array and returns the removed element.

const employee = ['Ajay', 'Vijay', 'Rahul'];

employee.shift();

console.log(employee);
Enter fullscreen mode Exit fullscreen mode

Output:['Vijay', 'Rahul']

The first element 'Ajay' is removed.

4. unshift() – Add Element to the Beginning

The unshift() method in JavaScript is used to add one or more elements to the beginning of an array.

It modifies the original array and returns the new length of the array.

const employee = ['Ajay', 'Vijay', 'Rahul'];

employee.unshift('Bala');

console.log(employee);
Enter fullscreen mode Exit fullscreen mode

Output: ['Bala', 'Ajay', 'Vijay', 'Rahul']

unshift() returns the new length of the array.

5. map() – Transform Array Data

The map() method in JavaScript is used to create a new array by applying a function to each element of an existing array.

Unlike push() or pop(), map() does not change the original array. It returns a new array.

let numbers = [1,2,3,4,5,6]
let result = numbers.map(n => n * 2);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:[2, 4, 6, 8, 10, 12]

let names = ['Ajay', 'Vijay', 'Rahul'];
let upperNames = names.map(empname => empname.toUpperCase());
console.log(upperNames);
Enter fullscreen mode Exit fullscreen mode

Output: ['AJAY', 'VIJAY', 'RAHUL']

This method is widely used in modern JavaScript frameworks like Angular and React.

6. filter() – Filter Data from an Array

The filter() method in JavaScript is used to create a new array containing elements that match a specific condition.

It does not modify the original array. Instead, it returns a new filtered array.

let numbers = [1,2,3,4,5,6]
let filteredNumbers = numbers.filter(n => n % 2 === 0);
console.log(filteredNumbers);
Enter fullscreen mode Exit fullscreen mode

Output: [2, 4, 6]

let numbers = [5,10,1,8,20,3,40]
let filteredNumbers = numbers.filter(n => n <= 10)
console.log(filteredNumbers)
Enter fullscreen mode Exit fullscreen mode

Output: [5, 10, 1, 8, 3]

7. reduce() – Reduce Array to a Single Value

The reduce() method in JavaScript is used to reduce an array to a single value by applying a function to each element.

It is commonly used for sum, totals, grouping, or complex calculations.

let numbers = [1,2,3,4,5,6]
let result = numbers.reduce((acc, num) => acc + num, 0);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output: 21

8. find() – Find First Matching Element

The find() method in JavaScript is used to return the first element in an array that satisfies a given condition.

If no element matches the condition, it returns undefined.

let numbers = [10,5,12,3,25,4,30]
let result = numbers.find(n => n > 10);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output: 12

9. includes() – Check if Value Exists

The includes() method in JavaScript is used to check whether an array contains a specific element.

It returns true if the element exists, otherwise false.

let names = ['Bala','Rahul','Sachin','Rohit'];
let result = names.includes('Bala');
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output: true

10. sort() – Sort Array Values

The sort() method in JavaScript is used to sort the elements of an array.
By default, it sorts elements as strings in ascending order and modifies the original array.

let numbers = [10,4,3,25,1,7,9];
let result = numbers.sort((a,b) => a-b);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output: [1, 3, 4, 7, 9, 10, 25]

JavaScript array methods make it easy to work with collections of data efficiently. Methods like map(), filter(), reduce(), and find() are especially important for modern web development and are widely used in frameworks like Angular, React, and Vue.

Check out more web development articles by clicking the link below.

Dev with Bala: Most Useful JavaScript Array Methods Every Developer Should Know

Most Useful HTML Tags Most Useful JavaScript Array Methods Every Developer Should Know JavaScript array me...

favicon devwithbala.blogspot.com

Top comments (0)