Array
The ordered, indexed collection. Arrays hold any mix of values and come with dozens of methods for adding, removing, transforming, and searching.
When to use it
- Ordered lists — items where position matters (todos, rows, steps)
- Iteration — looping through data to transform or filter it
- Stacks & queues —
push/popfor LIFO,push/shiftfor FIFO - Collecting results — building up output from a loop or API response
- Destructuring — pull specific positions into variables
Anatomy
const arr = [1, 2, 3]
arr.length // 3 — number of elements
arr[0] // 1 — access by index (zero-based)
arr.at(-1) // 3 — access from the end (ES2022)
// mutating
arr.push(4) // [1,2,3,4] — add to end, returns new length
arr.pop() // 4 — remove from end, returns removed item
arr.unshift(0) // [0,1,2,3] — add to start
arr.shift() // 0 — remove from start
arr.splice(1, 1) // removes 1 item at index 1, returns removed items
// non-mutating
arr.slice(0, 2) // [1,2] — shallow copy of a range
arr.concat([4, 5]) // [1,2,3,4,5] — merge arrays, returns new
arr.includes(2) // true — existence check
arr.indexOf(2) // 1 — first index of value, -1 if missing
arr.find(x => x > 1) // 2 — first match or undefined
arr.findIndex(x => x > 1) // 1 — index of first match or -1
// transforms (all return new arrays)
arr.map(x => x * 2) // [2,4,6]
arr.filter(x => x > 1) // [2,3]
arr.reduce((sum, x) => sum + x, 0) // 6
arr.flat() // flatten nested arrays one level
arr.flatMap(x => [x, x]) // map + flatten in one pass
// sorting
arr.sort((a, b) => a - b) // mutates — sorts in place
arr.toSorted((a, b) => a - b) // non-mutating copy (ES2023)
arr.reverse() // mutates — reverses in place
arr.toReversed() // non-mutating copy (ES2023)
// checks
Array.isArray(arr) // true — the reliable type check
arr.every(x => x > 0) // true — all pass?
arr.some(x => x > 2) // true — any pass?At a glance
| Method / Property | What it does |
|---|---|
.length | Number of elements |
[i] / .at(i) | Access by index (.at() supports negative) |
.push() / .pop() | Add/remove from end |
.unshift() / .shift() | Add/remove from start |
.splice(i, n) | Remove or insert at any position (mutates) |
.slice(start, end) | Shallow copy a range |
.concat() | Merge arrays into a new one |
.includes() | Boolean existence check |
.indexOf() | First index of value |
.find() / .findIndex() | First element/index matching a condition |
.map() | Transform every element → new array |
.filter() | Keep matching elements → new array |
.reduce() | Collapse to a single value |
.flat() / .flatMap() | Flatten nested arrays |
.sort() / .toSorted() | Sort in place / non-mutating sort |
.reverse() / .toReversed() | Reverse in place / non-mutating reverse |
.every() / .some() | All pass / any pass |
Array.isArray() | Reliable array type check |
1. Simple — filter and map
const nums = [1, 2, 3, 4, 5]
const doubled = nums
.filter(n => n % 2 === 0) // [2, 4] — keep only evens
.map(n => n * 2) // [4, 8] — double each one
// original array is untouched — filter and map return new arrays
nums // [1, 2, 3, 4, 5]2. Intermediate — destructuring and spread
const [first, ...rest] = [10, 20, 30]
first // 10 — first element pulled out
rest // [20, 30] — the rest collected into a new array
// combine arrays without mutating
const a = [1, 2]
const b = [3, 4]
const merged = [...a, ...b] // [1, 2, 3, 4]
// remove by index without splice (non-mutating)
const items = ['a', 'b', 'c', 'd']
const without = [...items.slice(0, 1), ...items.slice(2)]
without // ['a', 'c', 'd'] — removed index 13. Advanced — grouping with reduce
const people = [
{ name: 'Alice', dept: 'eng' },
{ name: 'Bob', dept: 'design' },
{ name: 'Carol', dept: 'eng' },
]
const byDept = people.reduce((groups, person) => {
const key = person.dept
groups[key] ??= [] // create the array if it doesn't exist
groups[key].push(person) // push into the correct group
return groups
}, {})
// { eng: [Alice, Carol], design: [Bob] }Object.groupBy() (ES2024) does this natively, but reduce is the classic approach and works everywhere.
Gotchas
Holes from delete
const arr = [1, 2, 3]
delete arr[1] // [1, empty, 3] — creates a hole, length stays 3
arr[1] // undefined — the slot exists but is empty
// use splice instead — it shifts elements and updates length
arr.splice(1, 1) // [1, 3] — actually removes the elementSort is lexicographic by default
[10, 9, 80].sort() // [10, 80, 9] — sorted as strings!
[10, 9, 80].sort((a, b) => a - b) // [9, 10, 80] — numeric sortRelated
- .map() — transform every element into a new array
- .filter() — keep elements that match a condition
- .reduce() — collapse an array into a single value
- .forEach() — loop without returning a new array
- Set — when you need unique values instead of an ordered list
Last updated on