JavaScript Array Methods Explorer
Interactive guide and cheat sheet for JavaScript array methods
JavaScript Array Methods Explorer is a visual guide to help you find the perfect array method for your needs. Compare map, filter, reduce, and more with examples and mutation details.
[1, 2, 3].map(x => x * 2); // [2, 4, 6][1, 2, 3, 4].filter(x => x > 2); // [3, 4][1, 2, 3, 4].reduce((acc, curr) => acc + curr, 0); // 10[1, 2, 3].forEach(x => console.log(x));[{id: 1}, {id: 2}].find(x => x.id === 2); // {id: 2}[10, 20, 30].findIndex(x => x > 15); // 1[1, 2, 3].some(x => x > 2); // true[1, 2, 3].every(x => x > 0); // true['apple', 'banana'].includes('apple'); // trueconst a = [1]; a.push(2); // a is [1, 2]const a = [1, 2]; a.pop(); // a is [1]const a = [1, 2]; a.shift(); // a is [2]const a = [1]; a.unshift(0); // a is [0, 1]const a = [1, 2, 3]; a.splice(1, 1, 'x'); // a is [1, 'x', 3][1, 2, 3, 4].slice(1, 3); // [2, 3][3, 1, 2].sort(); // [1, 2, 3]What is JavaScript Array Methods Explorer?
JavaScript Array Methods Explorer is an interactive cheat sheet designed to take the guesswork out of array manipulation. With so many methods available in modern JavaScript (ES6 and beyond), it's often hard to remember which one mutates the original array, which one returns a new array, or which one is most efficient for a specific task.
This tool provides a centralized database of the most commonly used array methods, complete with clear descriptions, use cases, and code examples.
How to Use
- Search and Filter: Use the search bar to find methods by name (e.g.,
reduce) or by behavior (e.g.,mutateortransform). - Review the Cards: Each card displays:
- Method Name: The exact name used in code.
- Mutation Badge: Green for IMMUTABLE (returns a new array/value) and Red for MUTATES (modifies the original array).
- Description: A plain-English explanation of what the method does.
- Returns: What the method actually gives you back.
- Best Use Case: When you should choose this method over others.
- Example: A short snippet illustrating the method in action.
- Quick Tags: Click on the common tags under the search bar to jump to specific categories of methods.
Basic Knowledge: Mutating vs. Non-Mutating
One of the most important distinctions in JavaScript array methods is whether they change (mutate) the original array or return a new one.
- Non-Mutating (Non-destructive): Methods like
map(),filter(),slice(), andconcat()do not change the original array. They return a brand new array. This is the preferred approach in modern frameworks like React. - Mutating (Destructive): Methods like
push(),pop(),sort(), andsplice()modify the original array directly. Use these when you want to save memory or are working in an environment where state mutations are expected.
Common "I want to..." Scenarios
- Transform every item: Use
map(). - Remove certain items: Use
filter(). - Find one specific item: Use
find(). - Check if any item matches: Use
some(). - Check if all items match: Use
every(). - Calculate a single value (e.g., sum): Use
reduce(). - Get a sub-section of an array: Use
slice().
FAQ
Q: Should I always avoid mutating methods?
A: In functional programming and React, yes. However, in plain JavaScript or performance-critical loops, mutating is sometimes faster.
Q: What is the difference between slice() and splice()?
A: slice() is non-mutating and returns a piece of the array. splice() is mutating and is used to add/remove/replace items at specific positions.
Q: Can I stop a forEach() loop?
A: No, you cannot break or return from a forEach() loop. If you need to stop early, use a standard for loop or methods like some() or every().
Q: Which method is fastest?
A: Performance varies by browser, but generally, standard for loops are fastest. However, for most applications, readability and maintainability (using map, filter, etc.) are more important than micro-optimizations.
Related Tools
- JavaScript Fetch Code Generator: Generate fetch() or axios code snippets for API requests
- JS Minifier Beautifier: Minify or beautify JavaScript code online
- JavaScript Object Path Finder: Quickly find and copy dot-notation paths for properties in large JSON objects
- JavaScript String Escaper: Escape or unescape special characters in JavaScript string literals
- JavaScript Truthy Falsy Visualizer: Inspect and visualize truthy/falsy values and type coercion in JavaScript
- JSON to JSDoc Converter: Automatically generate JSDoc type definitions from JSON data