Table of Contents
Quick Reference
| Method | What It Does | Changes the Original? |
| map() | Transforms every item into a new array | No |
| filter() | Keeps items that pass a test | No |
| reduce() | Combines everything into one value | No |
| find() | Returns the first matching item | No |
| includes() | Checks if a value exists | No |
| sort() | Reorders the array | Yes |
| reverse() | Flips the order | Yes |
| every() / some() | Checks all or some items against a test | No |
| join() | Turns the array into a string | No |
| concat() | Merges arrays into a new one | No |
| flat() | Flattens nested arrays | No |
Searching Methods
1.includes(), checks whether a value exists at all:
let scores = [90, 85, 70]; console.log(scores.includes(85)); // true2.indexOf(), returns a value's position, or -1 if it's missing:
console.log(scores.indexOf(70)); // 2 console.log(scores.indexOf(100)); // -13.find(), returns the first item matching a condition, genuinely handy with objects:
let users = [{ name: "Vi", age: 22 }, { name: "Sam", age: 30 }];
console.log(users.find(u => u.age > 25)); // { name: "Sam", age: 30 }
4.findIndex(), the same idea, but returns the position instead of the item:
console.log(users.findIndex(u => u.name === "Sam")); // 1
Transforming Methods
1.map(), builds a new array by transforming every item:let doubled = [1, 2, 3].map(n => n * 2); console.log(doubled); // [2, 4, 6]2.filter(), keeps only the items that pass a test:
let evens = [1, 2, 3, 4].filter(n => n % 2 === 0); console.log(evens); // [2, 4]3.reduce(), boils the whole array down into a single value, a sum, an object, anything:
let total = [1, 2, 3].reduce((sum, n) => sum + n, 0); console.log(total); // 64.flat(), flattens nested arrays into one level:
let nested = [1, [2, 3], [4, [5]]]; console.log(nested.flat()); // [1, 2, 3, 4, [5]] console.log(nested.flat(2)); // [1, 2, 3, 4, 5]
Sorting and Ordering
1.sort(), reorders items in place, but with a genuine trap, it compares as strings by default:console.log([10, 2, 33].sort()); // [10, 2, 33], wrong! console.log([10, 2, 33].sort((a, b) => a - b)); // [2, 10, 33], correct2.reverse(), flips the current order:
console.log([1, 2, 3].reverse()); // [3, 2, 1]Both of these change the original array directly, so if you need to keep the original order intact, copy it first with [...array] before sorting or reversing.
Checking Methods
1.every(), checks if all items pass a test:console.log([2, 4, 6].every(n => n % 2 === 0)); // true2.some(), checks if at least one item passes:
console.log([1, 3, 4].some(n => n % 2 === 0)); // true3.Array.isArray(), confirms whether something is actually an array, since typeof won't tell you that:
console.log(Array.isArray([1, 2, 3])); // true console.log(typeof [1, 2, 3]); // "object", not helpful here
Joining, Combining, and Converting
1.join(), turns an array into a string, with a separator of your choice:
console.log(["a", "b", "c"].join("-")); // "a-b-c"
2.concat(), merges arrays without changing the originals:
let merged = [1, 2].concat([3, 4]); console.log(merged); // [1, 2, 3, 4]3.The spread operator does the same job and is more commonly used in modern code:
let merged2 = [...[1, 2], ...[3, 4]]; console.log(merged2); // [1, 2, 3, 4]
Iterating
forEach(), runs a function on every item, doesn't return anything new:["a", "b", "c"].forEach(letter => console.log(letter));Use forEach() when you just need to do something with each item, like logging or updating something outside the array. Reach for map() instead the moment you actually need a new array back.
A Note on Mutating vs. Non-Mutating Methods
This is genuinely one of the most important things to internalize about array methods. Some change the original array directly, push, pop, splice, sort, and reverse all do this. Others leave the original completely untouched and hand you back a new array instead, map, filter, slice, and concat all fall here.let original = [3, 1, 2]; let mapped = original.map(n => n * 10); console.log(original); // [3, 1, 2], untouched console.log(mapped); // [30, 10, 20], new arrayMixing these up is a genuinely common source of bugs, especially when you expect an array to stay the same and it quietly doesn't.
Conclusion
You don't need to memorize every single array method to write good JavaScript, you really just need to recognize the pattern behind each group. Reach for map, filter, or reduce when you're transforming data, find or includes when you're searching, every or some when you're checking conditions, and keep half an eye on which methods quietly change your original array versus which ones hand you something new. Once that mental map is in place, picking the right method stops being a guessing game.Frequently Asked Questions (FAQs)
1.What's the difference between forEach() and map()?2.How do I know if a method changes my original array?forEach() just runs a function on each item and returns nothing useful. map() does the same thing but collects the results into a brand-new array, which is what you want whenever you actually need the transformed data afterward.
3.Which method should I use to check if an array contains a specific object, not just a primitive value?There's no universal rule in the naming, unfortunately, so it's worth checking documentation the first time you use a new method. As a shortcut, most of the newer, functional-style methods like map, filter, and slice don't mutate, while older, more direct ones like push, splice, and sort do.
includes() only works reliably for primitives like numbers and strings, since it checks by reference for objects. For objects, use find() or some() with a condition that compares the actual properties you care about.
0 Comments