Comprehensive reference for javascript array methods covering essential concepts, practical examples, and production best practices.
Javascript Array Methods are the foundational tools for manipulating, transforming, and querying arrays in JavaScript. Use them when you need to iterate, filter, transform, or reduce data in a predictable, functional way—whether processing API responses, building state updates in React, or preparing data for a charting library. These methods are not just convenient; they are the primary way to express intent in modern JavaScript.
The map method creates a new array by applying a function to each element of the original array.
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2);
// [2, 4, 6]
const users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 }
];
const userNames = users.map(u => u.name);
// ['Alice', 'Bob']
// Map with index and array context
const indexed = users.map((u, i, arr) => ({
id: i,
fullName: u.name,
isLast: i === arr.length - 1
}));
Key edge cases:
map does not mutate the original array; it returns a new array.element, index, array.undefined, that value is included in the output array (unlike forEach).map is lazy: it allocates a new array of the same length and fills it, but does not compact or resize it.Common failure:
map on sparse arrays: Array(5) with only arr[0] = 1 and arr[4] = 4 results in map producing a new array with 5 elements, where indices 1, 2, 3 are undefined.filter returns a new array containing only the elements that pass a test function.
const scores = [85, 92, 78, 96, 70];
const passing = scores.filter(s => s >= 80);
// [85, 92, 96]
const adults = users.filter(u => u.age >= 18);
// [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]
// Chaining with map
const highScores = scores
.filter(s => s > 80)
.map(s => s * 1.1) // bonus points
.filter(s => s > 100); // final cutoff
Key edge cases:
true or false (or any truthy/falsy value).filter preserves the original array's index structure, including holes.element, index, array.Common failure:
filter returns a Set or Map—it returns a plain array.filter does not modify the original array, leading to confusion when combining map and filter in complex pipelines.reduce applies a function to accumulate a value across the array.
const prices = [10, 15, 20, 5];
// Sum all prices
const total = prices.reduce((sum, price) => sum + price, 0);
// 50
// Group users by age
const grouped = users.reduce((acc, user) => {
const key = user.age;
if (!acc[key]) acc[key] = [];
acc[key].push(user);
return acc;
}, {});
// Result: { 30: [{ name: 'Alice', age: 30 }], 25: [{ name: 'Bob', age: 25 }] }
// Flatten nested arrays
const nested = [[1, 2], [3, 4], [5]];
const flat = nested.reduce((acc, arr) => acc.concat(arr), []);
// [1, 2, 3, 4, 5]
Key edge cases:
initialValue (second argument).initialValue is not provided and array is empty, reduce throws TypeError:> TypeError: Reduce of empty array with no initial value
accumulator, currentValue, index, array.reduce is not lazy—it always computes the entire result.Common failure:
reduce for map or filter—especially forgetting to return the updated accumulator.initialValue when the first element is not the initial state (e.g., reduce((a, b) => a + b) on [1] gives 1, but reduce((a, b) => a + b, 0) gives 1 for []).find returns the first element that satisfies a condition.
const users = [
{ name: 'Alice', role: 'admin' },
{ name: 'Bob', role: 'user' },
{ name: 'Charlie', role: 'admin' }
];
const admin = users.find(u => u.role === 'admin');
// { name: 'Alice', role: 'admin' }
const user = users.find(u => u.name === 'Charlie');
// { name: 'Charlie', role: 'admin' }
// Find with index
const firstUser = users.find((u, i) => u.role === 'user' && i === 0);
// { name: 'Bob', role: 'user' }
Key edge cases:
undefined if no element matches.element, index, array.find stops at the first match and does not continue.Common failure:
find with filter—expecting find to return an array, or using find when filter would suffice.undefined when find returns null or 0—especially when array elements are objects or numbers.findIndex returns the index of the first matching element.
const numbers = [10, 20, 30, 40];
const index = numbers.findIndex(n => n > 25);
// 2
const userIndex = users.findIndex(u => u.name === 'Bob');
// 1
// Find index of first even number
const evenIndex = numbers.findIndex(n => n % 2 === 0);
// 0
Key edge cases:
-1 if no element matches (not undefined).element, index, array.findIndex is fast and stops at the first match.Common failure:
findIndex returns undefined instead of -1—leading to bugs when using the result as an array index.findIndex to check existence: if (users.findIndex(u => u.name === 'Alice')) fails when Alice is at index 0.includes checks whether a value exists in the array.
const fruits = ['apple', 'banana', 'cherry'];
fruits.includes('banana');
// true
fruits.includes('grape');
// false
// With fromIndex
fruits.includes('apple', 1);
// false — starts search from index 1, so 'apple' not found
// With object references
const fruitsList = [fruits, ['kiwi', 'mango']];
fruitsList.includes(fruits);
// true
const fruitCopy = [...fruits];
fruitsList.includes(fruitCopy);
// false — different reference, even though values match
Key edge cases:
=== for primitive values (strings, numbers, booleans).SameValueZero algorithm for objects, arrays, and NaN.includes is case-sensitive for strings.fromIndex parameter starts the search from that index.Common failure:
includes works with deep equality—it does not. Two arrays with same elements are not equal unless they are the same object.includes on NaN arrays: arr.includes(NaN) works, but arr.includes(0) fails for NaN values.push adds one or more elements to the end of an array and returns the new length.
const tasks = ['learn JS', 'build API'];
const newLength = tasks.push('deploy', 'test');
// tasks = ['learn JS', 'build API', 'deploy', 'test']
// newLength = 4
Key edge cases:
Common failure:
push returns the length, not the array—leading to errors when chaining push with map or reduce.unshift adds one or more elements to the beginning of an array and returns the new length.
const logEvents = ['error', 'warning'];
const newLength = logEvents.unshift('info', 'debug');
// logEvents = ['info', 'debug', 'error', 'warning']
// newLength = 4
Key edge cases:
Common failure:
unshift with push—especially in loops where you want to prepend but use push.unshift to build a stack or queue efficiently.pop removes the last element from the array and returns it.
const stack = [1, 2, 3];
const last = stack.pop();
// last = 3, stack = [1, 2]
Key edge cases:
undefined if array is empty.Common failure:
pop on an empty array and not checking for undefined—leading to undefined values when expecting a value.pop returns the element, not the array, so you cannot chain pop calls.shift removes the first element and returns it.
const queue = ['first', 'second', 'third'];
const first = queue.shift();
// first = 'first', queue = ['second', 'third']
Key edge cases:
undefined if array is empty.Common failure:
shift when pop would be more appropriate, and vice versa—especially in queue and stack implementations.shift returns the array—leading to bugs when using the result in a pipeline.concat creates a new array by merging multiple arrays or values.
const a = [1, 2];
const b = [3, 4];
const combined = a.concat(b);
// [1, 2, 3, 4]
const withValues = a.concat(5, 6);
// [1, 2, 5, 6]
const nested = a.concat([b, [7, 8]]);
// [1, 2, [3, 4], [7, 8]]
Key edge cases:
Common failure:
concat with push—especially when trying to merge arrays but using push instead.concat flattens only the top level, not nested arrays.slice returns a shallow copy of a portion of the array.
const data = [10, 20, 30, 40, 50];
const subset = data.slice(1, 3);
// [20, 30]
const firstThree = data.slice(0, 3);
// [10, 20, 30]
const lastTwo = data.slice(-2);
// [40, 50]
// Clone array
const copy = data.slice();
// [10, 20, 30, 40, 50]
Key edge cases:
slice does not modify the original array.start index is inclusive, end index is exclusive.start defaults to 0, end defaults to array.length.-1 is last element.Common failure:
slice with splice—especially in state management where slice is used to derive a subset but splice is expected.slice returns a shallow copy—modifying nested objects in the slice affects the original array.splice modifies an array in place and returns the removed elements.
const colors = ['red', 'green', 'blue'];
// Remove 1 element from index 1
const removed = colors.splice(1, 1);
// colors = ['red', 'blue'], removed = ['green']
// Add elements at index 1
colors.splice(1, 0, 'yellow', 'purple');
// colors = ['red', 'yellow', 'purple', 'blue']
// Replace 2 elements starting at index 0
colors.splice(0, 2, 'orange', 'pink');
// colors = ['orange', 'pink', 'purple', 'blue'], removed = ['red', 'yellow']
Key edge cases:
start index.deleteCount.Common failure:
splice and slice—especially when thinking splice returns a new array.splice returns the removed elements, not the modified array.splice with deleteCount but not providing elements—leading to unexpected removals without insertions.reverse reverses the array in place and returns the same array.
const items = ['a', 'b', 'c'];
const reversed = items.reverse();
// items = ['c', 'b', 'a'], reversed = ['c', 'b', 'a']
Key edge cases:
Common failure:
reverse returns a new array—leading to confusion when using the result in a pipeline.reverse mutates the original array, causing side effects.sort sorts elements in place and returns the sorted array.
const nums = [10, 1, 25, 3];
nums.sort();
// [1, 10, 25, 3] — lexicographic (string) sort
const names = ['Alice', 'Bob', 'Charlie'];
names.sort();
// ['Alice', 'Bob', 'Charlie'] — alphabetical
// Custom sort
const numbers = [4, 1, 3, 2];
numbers.sort((a, b) => a - b);
// [1, 2, 3, 4]
Key edge cases:
localeCompare for strings.sort uses toString() to compare elements—so 10 and 1 are compared as strings.a, b and returns:a comes before ba and b are equala comes after bCommon failure:
sort works with numbers as expected—leading to [1, 10, 2, 20] instead of [1, 2, 10, 20].a - b in a numeric sort—leading to incorrect ordering.This page was rewritten on 10 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy