🧠 JavaScript Array Methods Every Developer Should Know


Arrays are everywhere—they help us store, manage, and manipulate lists of data. In this guide, we’ll dive deep into the most essential array methods everyone should know.
To make things easier, we’ve grouped the methods into logical categories:
Iteration & Transformation
Modification
Searching and Finding
Utility and Manipulation
🌀 1. Iteration & Transformation
These methods help you loop over arrays and transform their elements. They’re the backbone of clean, readable, functional JavaScript.
🔁 forEach()
Purpose: Executes a provided function once for each array element.
When to use: When you want to perform a side-effect (like logging or updating something) on each element, but don’t need to return a new array.
Use case: Printing elements, updating the DOM, and triggering API calls for each item.
Syntax:
array.forEach(callback(currentValue, index, array))
const fruits = ['apple', 'banana', 'cherry'];
fruits.forEach(fruit => {
console.log(`I like ${fruit}`);
});
// Output:
// I like apple
// I like banana
// I like cherry
❗ Note: forEach doesn’t return anything, so you can’t chain it like map.
🔄 map()
Purpose: Creates a new array by applying a function to each element.
When to use: When you need to transform data without changing the original array.
Syntax:
const newArray = array.map(callback(currentValue, index, array))
const prices = [100, 200, 300];
const withTax = prices.map(price => price * 1.18);
console.log(withTax); // [118, 236, 354]
Use Case: Adding tax to product prices, formatting user names, or transforming data before rendering.
📌 Important: map() does not change the original array; it returns a new one.
🧹 filter()
Purpose: Returns a new array containing only elements that pass a condition.
When to use: When you need a subset of items from an array based on a test.
Syntax:
const filteredArray = array.filter(callback(currentValue, index, array))
const ages = [12, 17, 20, 25];
const adults = ages.filter(age => age >= 18);
console.log(adults); // [20, 25]
Use Case: Filtering out inactive users, expired products, or unwanted data.
🧮 reduce()
Purpose: Reduces an array to a single value by accumulating results.
When to use: When you need to calculate a total, merge items, or produce a result from all elements.
Syntax:
const result = array.reduce((accumulator, currentValue, index, array) => {
return accumulator + currentValue;
}, initialValue)
const numbers = [10, 20, 30];
const total = numbers.reduce((sum, num) => sum + num, 0);
console.log(total); // 60
Use Case: Summing orders, aggregating statistics, or flattening nested arrays.
2. ✏️ Modification Methods
These methods directly change (mutate) the original array. Use them with care in scenarios where state matters.
➕ push() and ➖ pop()
push()
Adds items to the end. pop()
Removes the last item.
const stack = [1, 2];
stack.push(3); // [1, 2, 3]
stack.pop(); // [1, 2]
Use Case: Managing history (undo-redo), adding new chat messages, or simulating stacks.
⬅️ unshift() and ➡️ shift()
unshift()
adds to the start. shift()
Removes from the start.
const queue = ['first'];
queue.unshift('zero'); // ['zero', 'first']
queue.shift(); // ['first']
Use Case: Managing queues, front-loading items, or updating recent activity.
✂️ splice()
Purpose: Adds, removes, or replaces elements in an array at any position.
Syntax:
array.splice(startIndex, deleteCount, item1, item2, ...)
const items = ['a', 'b', 'c'];
items.splice(1, 1, 'x', 'y');
console.log(items); // ['a', 'x', 'y', 'c']
Use Case: Inserting new tasks into a to-do list or editing items at runtime.
🧯 fill()
Purpose: Replaces all or some values in the array with a static value.
const filled = new Array(3).fill('X');
console.log(filled); // ['X', 'X', 'X']
Use Case: Preloading placeholders or resetting arrays.
✅ some() and ✅ every()
some()
Checks if at least one element passes the test.every()
Checks if all elements pass the test.const scores = [85, 90, 78]; console.log(scores.some(score => score < 80)); // true console.log(scores.every(score => score >= 60)); // true
Use Case: Validation checks or feature toggles.
3. 🔍 Searching and Finding Methods
These help locate values or positions within arrays.
🔎 find()
Purpose: Returns the first element that matches a condition.
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
const user = users.find(u => u.id === 2);
console.log(user); // { id: 2, name: 'Bob' }
Use Case: Finding a user by ID or a product by SKU.
✅ includes()
Purpose: Checks if a value exists in the array.
const list = ['milk', 'bread', 'butter'];
console.log(list.includes('bread')); // true
Use Case: Checking selections or toggling favourites.
🔢 indexOf()
Purpose: Returns the index of the first occurrence or -1 if not found.
const letters = ['a', 'b', 'a'];
console.log(letters.indexOf('a')); // 0
Use Case: Finding positions or checking duplicates.
4. 🧰 Utility and Manipulation Methods
These provide flexible ways to combine, clone, or convert arrays.
🔗 join()
Purpose: Joins array elements into a single string.
const words = ['Hello', 'World'];
console.log(words.join(' ')); // "Hello World"
Use Case: Creating display strings, CSVs, or URLs.
📊 sort()
Purpose: Sorts array elements in place.
const nums = [3, 1, 4];
nums.sort((a, b) => a - b);
console.log(nums); // [1, 3, 4]
Use Case: Sorting scores, names, or dates.
📐 slice()
Purpose: Returns a shallow copy of part of the array.
const colors = ['red', 'green', 'blue', 'yellow'];
console.log(colors.slice(1, 3)); // ['green', 'blue']
Use Case: Pagination, previews, or trimming lists.
🔁 concat()
Purpose: Merges two or more arrays into a new one.
const a = [1, 2];
const b = [3, 4];
console.log(a.concat(b)); // [1, 2, 3, 4]
Use Case: Combining results from different sources.
🔡 toString()
Purpose: Converts an array to a comma-separated string.
const arr = [1, 2, 3];
console.log(arr.toString()); // "1,2,3"
Subscribe to my newsletter
Read articles from Pavan Kumar Siriyala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
