Arrays & Array Methods
📖 Concept
Arrays are ordered collections of values. They can hold any type — numbers, strings, objects, even other arrays.
Mutating methods (change the original): push, pop, shift, unshift, splice, sort, reverse, fill
Non-mutating methods (return new): map, filter, reduce, slice, concat, flat, flatMap, find, some, every, includes
The "Big 3" higher-order array methods:
- map — Transform each element → new array of same length
- filter — Keep elements that pass a test → new array (possibly shorter)
- reduce — Accumulate all elements into a single value
🏠 Real-world analogy: map is like a factory assembly line — every item gets processed. filter is quality control — only good items pass. reduce is melting everything into one bar of gold.
💻 Code Example
1const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];23// map — transform every element4const doubled = numbers.map(n => n * 2);5// [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]67// filter — keep elements matching a condition8const evens = numbers.filter(n => n % 2 === 0);9// [2, 4, 6, 8, 10]1011// reduce — accumulate into a single value12const sum = numbers.reduce((acc, n) => acc + n, 0); // 551314// find — first element matching condition15const firstOver5 = numbers.find(n => n > 5); // 61617// some & every — boolean checks18const hasNeg = numbers.some(n => n < 0); // false19const allPos = numbers.every(n => n > 0); // true2021// forEach — side effects (no return value!)22numbers.forEach((n, i) => {23 console.log(`Index ${i}: ${n}`);24});2526// Chaining methods27const result = numbers28 .filter(n => n % 2 === 0) // [2, 4, 6, 8, 10]29 .map(n => n ** 2) // [4, 16, 36, 64, 100]30 .reduce((sum, n) => sum + n, 0); // 2203132// Useful patterns33const users = [34 { name: "Alice", age: 25 },35 { name: "Bob", age: 17 },36 { name: "Charlie", age: 30 }37];3839// Group by (using reduce)40const grouped = users.reduce((groups, user) => {41 const key = user.age >= 18 ? "adults" : "minors";42 groups[key] = groups[key] || [];43 groups[key].push(user);44 return groups;45}, {});4647// Flat & flatMap48const nested = [[1, 2], [3, [4, 5]]];49console.log(nested.flat()); // [1, 2, 3, [4, 5]]50console.log(nested.flat(Infinity)); // [1, 2, 3, 4, 5]5152// Array.from — create arrays from iterables53const chars = Array.from("hello"); // ['h','e','l','l','o']54const range = Array.from({length: 5}, (_, i) => i + 1); // [1,2,3,4,5]
🏋️ Practice Exercise
Mini Exercise:
- Given an array of objects with name and score, return names of those who scored above 80
- Use
reduceto count the frequency of each word in an array - Write a function to remove duplicates from an array (3 different ways)
- Implement your own
mapfunction usingreduce - Flatten a deeply nested array without using
.flat()
⚠️ Common Mistakes
forEachdoes NOT return a new array — usemapif you need a transformed arrayForgetting that
sort()sorts as STRINGS by default —[10, 2, 1].sort()→[1, 10, 2]! Use.sort((a,b) => a - b)Using
mapwhen you should useforEach(for side effects) orfilter(for subsetting)Mutating the original array with
splice,sort,reversewhen you didn't intend to — useslice,toSorted,toReversedForgetting the initial value in
reduce— without it, the first element becomes the accumulator, which fails on empty arrays
💼 Interview Questions
🎤 Mock Interview
Mock interview is powered by AI for Arrays & Array Methods. Login to unlock this feature.