10 JavaScript Array Methods You'll Actually Use Every Day
Master the most practical JavaScript array methods with real-world examples. Learn map, filter, reduce, and other essential array operations for everyday coding.
JavaScript arrays are everywhere, but you don't need to learn all 30+ methods. Here are the ones you'll actually use in real projects.
map() - Transform Data
Perfect for converting data from one format to another.
const prices = [10, 25, 30, 45];
const formatted = prices.map(price => `$${price.toFixed(2)}`);
// Result: ['$10.00', '$25.00', '$30.00', '$45.00']
const users = [
{ id: 1, name: 'John', email: 'john@example.com' },
{ id: 2, name: 'Jane', email: 'jane@example.com' }
];
const usernames = users.map(user => user.name);
// Result: ['John', 'Jane']
filter() - Find Matching Items
When you need specific items that match certain criteria.
const users = [
{ id: 1, name: 'John', active: true },
{ id: 2, name: 'Jane', active: false },
{ id: 3, name: 'Bob', active: true }
];
const activeUsers = users.filter(user => user.active);
// Result: [{ id: 1, name: 'John'... }, { id: 3, name: 'Bob'... }]
find() - Get First Match
Like filter(), but returns just the first match. Perfect for IDs and unique values.
const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' }
];
const user = users.find(user => user.id === 2);
// Result: { id: 2, name: 'Jane' }
reduce() - Calculate or Combine
Great for totals and transforming arrays into different structures.
const cart = [
{ item: 'Book', price: 20 },
{ item: 'Pen', price: 5 },
{ item: 'Notebook', price: 15 }
];
const total = cart.reduce((sum, item) => sum + item.price, 0);
// Result: 40
const products = [
{ category: 'Electronics', name: 'Laptop' },
{ category: 'Books', name: 'JavaScript Guide' },
{ category: 'Electronics', name: 'Phone' }
];
const grouped = products.reduce((groups, item) => {
groups[item.category] = groups[item.category] || [];
groups[item.category].push(item.name);
return groups;
}, {});
// Result: {
// Electronics: ['Laptop', 'Phone'],
// Books: ['JavaScript Guide']
// }
sort() - Order Items
Remember: sort() modifies the original array. Make a copy first if you need the original order.
const numbers = [23, 5, 100, 56, 9, 13];
const sorted = [...numbers].sort((a, b) => a - b);
// Result: [5, 9, 13, 23, 56, 100]
const users = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 },
{ name: 'Bob', age: 35 }
];
const byAge = [...users].sort((a, b) => a.age - b.age);
// Result: [{ name: 'Jane'... }, { name: 'John'... }, { name: 'Bob'... }]
some() - Check if Any Match
Perfect for validation and checking conditions.
const users = [
{ name: 'John', premium: false },
{ name: 'Jane', premium: true }
];
const hasPremium = users.some(user => user.premium);
// Result: true
every() - Check if All Match
Great for validation where all items must pass.
const formData = [
{ field: 'name', value: 'John' },
{ field: 'email', value: 'john@example.com' },
{ field: 'age', value: '' }
];
const allFilled = formData.every(field => field.value !== '');
// Result: false
slice() - Get Part of Array
Get a portion without changing the original. Great for pagination.
const items = ['A', 'B', 'C', 'D', 'E', 'F'];
const pageSize = 2;
const page = 1;
const pageItems = items.slice(page * pageSize, (page + 1) * pageSize);
// Result: ['C', 'D']
includes() - Simple Search
For basic searching in simple arrays.
const permissions = ['read', 'write', 'delete'];
const canWrite = permissions.includes('write');
// Result: true
at() - Get by Index
Works with negative numbers for counting from end. Newer but super useful.
const items = ['first', 'second', 'third', 'last'];
const lastItem = items.at(-1); // 'last'
const secondLast = items.at(-2); // 'third'
These methods handle 90% of array operations in real projects. Combine them for more complex operations:
const products = [
{ id: 1, name: 'Laptop', price: 999, inStock: true },
{ id: 2, name: 'Phone', price: 699, inStock: false },
{ id: 3, name: 'Tablet', price: 399, inStock: true }
];
const availableProducts = products
.filter(product => product.inStock)
.map(product => ({
name: product.name,
priceWithTax: product.price * 1.2
}))
.sort((a, b) => a.priceWithTax - b.priceWithTax);
Remember: Most of these methods create new arrays instead of modifying the original. This is good for avoiding bugs, but watch your memory usage when working with large datasets.