JavaScript arrays have a rich set of functional methods. All return new arrays (immutable style) except sort() and reverse() which mutate in place. Use slice() to copy first.
| Name | Signature / Syntax | Description |
|---|---|---|
| push / pop | arr.push(x) / arr.pop() | Add/remove from end |
| shift / unshift | arr.shift() / arr.unshift(x) | Remove/add at start |
| splice | arr.splice(start, deleteCount, ...items) | Remove/insert at position (mutates) |
| slice | arr.slice(start, end) → newArr | Return shallow copy of portion |
| concat | arr.concat(arr2) → newArr | Return new array with arr2 appended |
| map | arr.map(fn) → newArr | Transform each element |
| filter | arr.filter(fn) → newArr | Keep elements where fn returns true |
| reduce | arr.reduce((acc,val) => acc+val, init) | Fold to single value |
| reduceRight | arr.reduceRight(fn, init) | Reduce from right to left |
| find | arr.find(fn) → element | First element where fn is true |
| findIndex | arr.findIndex(fn) → index | Index of first match |
| findLast | arr.findLast(fn) → element | Last element where fn is true (ES2023) |
| some | arr.some(fn) → bool | True if any element matches |
| every | arr.every(fn) → bool | True if all elements match |
| includes | arr.includes(val) → bool | True if array contains val |
| indexOf | arr.indexOf(val) → index | First index of val (-1 if absent) |
| flat | arr.flat(depth) → newArr | Flatten nested arrays |
| flatMap | arr.flatMap(fn) → newArr | Map then flatten one level |
| sort | arr.sort((a,b) => a-b) | Sort in place — always pass comparator for numbers! |
| reverse | arr.reverse() | Reverse in place |
| forEach | arr.forEach(fn) | Execute fn for each element (no return value) |
| join | arr.join(sep) → string | Join elements into string |
| fill | arr.fill(val, start, end) | Fill with value in range |
| Array.from | Array.from(iterable, mapFn) | Create array from iterable or array-like |
| Array.of | Array.of(1,2,3) → [1,2,3] | Create array from arguments |
| Array.isArray | Array.isArray(val) → bool | True if val is an Array |
| entries/keys/values | arr.entries() / .keys() / .values() | Iterator over [index,value] / indices / values |
| at | arr.at(-1) | Access by index, supports negative (ES2022) |
| toSorted/toReversed | arr.toSorted() / arr.toReversed() | Non-mutating sort/reverse (ES2023) |
| structuredClone | structuredClone(obj) | Deep clone any serializable object |
Run with node script.js or save as HTML and open in browser.
// Array methods — functional style
const students = [
{ name: 'Alice', score: 95, subject: 'Math' },
{ name: 'Bob', score: 87, subject: 'Science' },
{ name: 'Carol', score: 92, subject: 'Math' },
{ name: 'Dave', score: 78, subject: 'Science' },
{ name: 'Eve', score: 98, subject: 'Math' },
];
// filter + map + sort
const topMath = students
.filter(s => s.subject === 'Math' && s.score >= 90)
.map(s => `\${s.name}(\${s.score})`)
.sort();
console.log('Top Math:', topMath);
// reduce: group by subject
const bySubject = students.reduce((acc, s) => {
(acc[s.subject] = acc[s.subject] || []).push(s);
return acc;
}, {});
for (const [subj, group] of Object.entries(bySubject)) {
const avg = group.reduce((sum, s) => sum + s.score, 0) / group.length;
console.log(`\${subj}: avg=\${avg.toFixed(1)} n=\${group.length}`);
}
// find / findIndex
const best = students.reduce((a, b) => a.score > b.score ? a : b);
console.log('Best:', best.name, best.score);
// flat / flatMap
const matrix = [[1,2,3],[4,5,6],[7,8,9]];
console.log(matrix.flat());
const pairs = [1,2,3].flatMap(n => [n, n*n]);
console.log(pairs); // [1,1,2,4,3,9]
// Array.from with map
const squares = Array.from({length:10}, (_, i) => (i+1)**2);
console.log(squares);
// Immutable sort (ES2023)
const sorted = students.toSorted((a,b) => b.score - a.score);
console.log('Sorted (immutable):', sorted.map(s=>s.name));
// node script.js