JavaScript - Array methods cheat sheet
15+ array methods to ace your JavaScript interview
No bullsh%t, Only array methods for quick review
Name | Description | Example |
map | Creates a new array with the results of calling a provided function on every element. | const numbers = [1, 2, 3]; const doubled = numbers.map(x => x * 2); // [2, 4, 6] |
find | Returns the first element that satisfies the provided testing function. | const numbers = [1, 2, 3]; const found = numbers.find(x => x > 2); // 3 |
reduce | Applies a function against an accumulator and each element to reduce it to a single value. | const numbers = [1, 2, 3]; const sum = numbers.reduce((acc, val) => acc + val, 0); // 6 |
some | Tests whether at least one element in the array passes the test implemented by the function. | const numbers = [1, 2, 3]; const hasEven = numbers.some(x => x % 2 === 0); // true |
every | Tests whether all elements in the array pass the test implemented by the provided function. | const numbers = [1, 2, 3]; const allEven = numbers.every(x => x % 2 === 0); // false |
filter | Creates a new array with all elements that pass the test implemented by the provided function. | const numbers = [1, 2, 3]; const evens = numbers.filter(x => x % 2 === 0); // [2] |
forEach | Executes a provided function once for each array element. | const numbers = [1, 2, 3]; numbers.forEach(x => console.log(x * 2)); // 2, 4, 6 |
findIndex | Returns the index of the first element that satisfies the provided testing function. | const numbers = [1, 2, 3]; const index = numbers.findIndex(x => x > 2); // 2 |
includes | Determines whether an array includes a certain value, returning true or false. | const numbers = [1, 2, 3]; const hasTwo = numbers.includes(2); // true |
from | Creates a new array instance from an array-like or iterable object. | const set = new Set([1, 2, 3]); const array = Array.from(set); // [1, 2, 3] |
of | Creates a new Array instance with a variable number of arguments. | const array = Array.of(1, 2, 3); // [1, 2, 3] |
flat | Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. | const nestedArray = [1, [2, [3]]]; const flatArray = nestedArray.flat(2); // [1, 2, 3] |
flatMap | Maps each element using a mapping function, then flattens the result into a new array. | const arr = [1, 2]; const flatMapped = arr.flatMap(x => [x, x * 2]); // [1, 2, 2, 4] |
concat | Merges two or more arrays, returning a new array. | const array1 = [1, 2]; const array2 = [3, 4]; const newArray = array1.concat(array2); // [1, 2, 3, 4] |
slice | Returns a shallow copy of a portion of an array into a new array object. | const array = [1, 2, 3, 4]; const sliced = array.slice(1, 3); // [2, 3] |
splice | Changes the contents of an array by removing or replacing existing elements and/or adding new elements. | const array = [1, 2, 3, 4]; array.splice(1, 2, 'a', 'b'); // [1, 'a', 'b', 4] |
Save this blog now for your interview preps or quick revision!
I get it. You don't wanna read long blogs just to find a gist. So here, I have created a quick checklist of array methods that are pretty useful for your next interview prep or a quick review. Anyway, if I've missed something, comment it and I'll edit the blog.
If you liked it, you know the drill - like, save, subscribe. Until next time, adios amigos!
About the author
Hi, I am Mariya Zaveri. My passion for JavaSript started when I started learning React before knowing anything about JavaScript. I was frustrated all the time and did not know why JS behaved so weirdly in every situation. That’s when I decided to start exploring the language to its core.
Little did I know that doing this would help me develop a keen interest in web development and JavaScript's intricacies. This blog was born in my attempt to simplify complex coding concepts, making them accessible to both beginners and experienced developers.
My goal is to empower you to become proficient in JavaScript and create amazing web applications. You can connect with me on my Twitter, LinkedIn, GitHub or subscribe to my blog to stay updated with my latest coding adventures and insights.