Array Methods
This lesson explains Array Methods in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.
JavaScript Array Methods Overview
JavaScript array methods are built-in functions that simplify working with
arrays. They allow developers to add, remove, search, sort, transform,
filter, and aggregate data efficiently without writing complex loops.
Modern JavaScript applications heavily rely on array methods for processing
API responses, rendering user interfaces, managing application state, and
transforming data. Mastering these methods is essential for frontend,
backend, and full-stack JavaScript development.
| Method Category | Purpose | Examples |
| Add Elements | Insert new items. | push, unshift |
| Remove Elements | Delete existing items. | pop, shift, splice |
| Search | Locate items. | find, includes, indexOf |
| Transform | Create a new array. | map |
| Filter | Select matching items. | filter |
| Aggregate | Calculate a single value. | reduce |
| Sort | Arrange array elements. | sort, reverse |
JavaScript Array Methods Example
const numbers = [10, 20, 30];
numbers.push(40);
const doubled = numbers.map(function(number) {
return number * 2;
});
const filtered = numbers.filter(function(number) {
return number > 20;
});
console.log(numbers);
console.log(doubled);
console.log(filtered);
This example demonstrates some of the most commonly used array methods,
including push(), map(), and
filter(), which are frequently used in modern JavaScript
applications.
Top 10 JavaScript Array Method Examples
The following examples demonstrate the most commonly used JavaScript array
methods in real-world applications.
| # | Method | Syntax |
| 1 | Add Item | fruits.push("Orange"); |
| 2 | Remove Last Item | fruits.pop(); |
| 3 | Remove First Item | fruits.shift(); |
| 4 | Add First Item | fruits.unshift("Apple"); |
| 5 | Transform Values | numbers.map(number => number * 2); |
| 6 | Filter Values | numbers.filter(number => number > 10); |
| 7 | Find First Match | users.find(user => user.id === 1); |
| 8 | Calculate Total | prices.reduce((sum, price) => sum + price, 0); |
| 9 | Sort Array | numbers.sort((a, b) => a - b); |
| 10 | Check Value Exists | fruits.includes("Apple"); |
Complete JavaScript Array Methods with Output
| Method | Description | Example | Output |
at() | Returns an element by index. | [10,20,30].at(-1) | 30 |
concat() | Combines arrays. | [1,2].concat([3,4]) | [1,2,3,4] |
copyWithin() | Copies part of an array. | [1,2,3,4].copyWithin(0,2) | [3,4,3,4] |
entries() | Returns an iterator. | [10,20].entries() | Iterator |
every() | Checks if all values match. | [2,4,6].every(n => n % 2 === 0) | true |
fill() | Fills an array. | [1,2,3].fill(0) | [0,0,0] |
filter() | Returns matching values. | [5,10,15].filter(n => n > 8) | [10,15] |
find() | Returns first match. | [5,10,15].find(n => n > 8) | 10 |
findIndex() | Returns first matching index. | [5,10,15].findIndex(n => n > 8) | 1 |
findLast() | Returns last matching value. | [5,10,15].findLast(n => n > 8) | 15 |
findLastIndex() | Returns last matching index. | [5,10,15].findLastIndex(n => n > 8) | 2 |
flat() | Flattens nested arrays. | [1,[2,3]].flat() | [1,2,3] |
flatMap() | Maps and flattens. | [1,2].flatMap(n => [n,n]) | [1,1,2,2] |
forEach() | Executes a callback. | [1,2].forEach(console.log) | undefined |
includes() | Checks whether a value exists. | [1,2,3].includes(2) | true |
indexOf() | Returns first index. | [5,10,15].indexOf(10) | 1 |
join() | Creates a string. | ["A","B"].join("-") | "A-B" |
keys() | Returns index iterator. | [10,20].keys() | Iterator |
lastIndexOf() | Returns last index. | [1,2,1].lastIndexOf(1) | 2 |
map() | Transforms values. | [1,2,3].map(n => n * 2) | [2,4,6] |
pop() | Removes last item. | [1,2,3].pop() | 3 |
push() | Adds an item. | arr.push(4) | New length |
reduce() | Calculates a single value. | [1,2,3].reduce((a,b)=>a+b) | 6 |
reduceRight() | Reduces from right. | [1,2,3].reduceRight((a,b)=>a+b) | 6 |
reverse() | Reverses array. | [1,2,3].reverse() | [3,2,1] |
shift() | Removes first item. | [1,2,3].shift() | 1 |
slice() | Returns a portion. | [1,2,3,4].slice(1,3) | [2,3] |
some() | Checks if any value matches. | [1,3,5].some(n => n % 2 === 0) | false |
sort() | Sorts elements. | [3,1,2].sort() | [1,2,3] |
splice() | Adds or removes items. | [1,2,3].splice(1,1) | [2] |
toReversed() | Returns reversed copy. | [1,2,3].toReversed() | [3,2,1] |
toSorted() | Returns sorted copy. | [3,1,2].toSorted() | [1,2,3] |
toSpliced() | Returns modified copy. | [1,2,3].toSpliced(1,1) | [1,3] |
toString() | Converts to string. | [1,2,3].toString() | "1,2,3" |
unshift() | Adds item at beginning. | arr.unshift(0) | New length |
values() | Returns value iterator. | [10,20].values() | Iterator |
with() | Returns updated copy. | [1,2,3].with(1,100) | [1,100,3] |
Best Practices
- Prefer array methods over manual loops when they improve readability.
- Use
map() for transforming arrays. - Use
filter() to create a filtered array. - Use
find() when searching for a single element. - Use
reduce() for totals and accumulated values. - Avoid mutating arrays unless necessary.
- Use descriptive callback parameter names.
- Choose the method that best matches the operation being performed.
Common Mistakes
- Using
map() without returning a value. - Using
forEach() when a transformed array is needed. - Using
filter() instead of find() for one result. - Sorting numbers without providing a compare function.
- Modifying the original array unexpectedly.
- Using
splice() when slice() is intended. - Ignoring the return value of array methods.
Key Takeaways
- Array methods simplify data manipulation.
push() and pop() add and remove items. map() transforms data into a new array. filter() selects matching elements. find() returns the first matching element. reduce() combines multiple values into a single result. - Modern JavaScript development relies heavily on array methods.
Pro Tip
Master map(), filter(),
find(), and reduce(). These four methods are
among the most frequently used JavaScript features in React, Angular,
Vue, Node.js, interviews, and production applications.