0% found this document useful (0 votes)
159 views2 pages

JavaScript Array Methods Cheat Sheet

This cheat sheet provides an overview of JavaScript array methods, categorizing them into callback-based methods and non-callback methods. It lists various methods along with their functionalities, such as forEach, map, and filter for callbacks, and push, pop, and slice for non-callbacks. A summary table highlights features like whether methods use callbacks, return new arrays, or mutate the original array.

Uploaded by

ekansh kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views2 pages

JavaScript Array Methods Cheat Sheet

This cheat sheet provides an overview of JavaScript array methods, categorizing them into callback-based methods and non-callback methods. It lists various methods along with their functionalities, such as forEach, map, and filter for callbacks, and push, pop, and slice for non-callbacks. A summary table highlights features like whether methods use callbacks, return new arrays, or mutate the original array.

Uploaded by

ekansh kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Array Methods - Cheat Sheet

1. Callback-Based Methods

Methods that Accept Callback:

- forEach(): Runs a function on each element (no return).

- map(): Returns a new array after applying a function to each element.

- filter(): Returns elements that pass a test.

- find(): Returns the first element that passes a test.

- findIndex(): Returns index of first element that passes a test.

- some(): Returns true if at least one element passes a test.

- every(): Returns true if all elements pass a test.

- reduce(): Reduces array to a single value (left-to-right).

- reduceRight(): Like reduce, but right-to-left.

- flatMap(): Maps and flattens array one level.

- sort(): Sorts array (can use custom comparator).

2. Non-Callback Methods

Methods that Do Not Accept Callback:

- push(), pop(), shift(), unshift(), splice(): Modify array content.

- slice(), concat(), flat(): Return a new array (non-mutating).

- reverse(), fill(), copyWithin(): Modify original array.

- join(), toString(), includes(), indexOf(), lastIndexOf(), at(): Do not mutate.

3. Quick Summary

Summary Table:

Feature | Methods

------------------|----------------------------------------------------------

Page 1 - Generated on 2025-05-13


JavaScript Array Methods - Cheat Sheet

Uses callback | forEach, map, filter, find, reduce, etc.

No callback | push, pop, splice, slice, flat, at, join, etc.

Returns new array | map, filter, slice, flat, concat, flatMap

Mutates array | push, pop, splice, sort, reverse, fill, etc.

Page 2 - Generated on 2025-05-13

Common questions

Powered by AI

The primary benefit of using the forEach() method is its simplicity and readability for situations requiring executions of side effects for each element. It doesn't return a new array, making it less suitable for operations that transform data or filter elements. Compared to map(), which is designed to transform each element of the array and return a new array or filter(), which selectively returns elements, forEach() has the drawback of not being useful for these more complex data manipulation tasks. Its design is focused on executing a function on each element without concern for returning data .

The reduce() method operates by accumulating values starting from the first element towards the last, while reduceRight() processes the array from the last element to the first. This directional processing difference can impact applications where the accumulation logic depends on element order, such as evaluating expressions that differ in their operand order. ReduceRight is useful in scenarios that involve hierarchical structures or operations like balancing parentheses in reversal order, whereas reduce is suitable for most left-to-right accumulation tasks .

The includes() method simplifies the process of checking whether an array contains a certain element by offering a straightforward syntax that returns a boolean value. Compared to traditional loops or the use of indexOf() (which requires additional checks against -1 for non-existence), includes() provides a more concise and readable solution. This readability reduces the cognitive load on developers, allowing them to quickly ascertain the membership of an element without verbose code constructs .

Using the sort() method with a custom comparator function allows developers to define specific sorting logic beyond the default ASCII character sorting. This method directly mutates the original array by reordering its elements based on the comparator's return values. The side effect of this mutability is that any references to the original array reflect these changes, which might lead to unexpected results if the original order is still needed later in the program. It is crucial to understand these implications to avoid accidentally altering data structures used elsewhere .

The flat() method simplifies an array containing nested arrays by reducing the hierarchy of arrays by one specified level, enabling smoother access to elements within nested structures. This transformation assists in normalizing data structures that naturally contain depth, such as JSON document representations. By flattening nested arrays, the method emphasizes enhancing manipulation and iteration over initially complex nested forms, though care must be taken to ensure the intended depth level aligns with how data needs to be processed .

The reduce() method is preferable when you need to accumulate a single result from the elements of an array, rather than transforming or selecting elements. It is particularly useful in scenarios where you need to compute cumulative values like sums, products, or to construct an object based on array elements. Unlike map() or filter(), reduce() works by carrying an accumulator and iterating from left to right (or right to left, with reduceRight()). This capability makes it ideal for aggregating data into a singular output form .

The flatMap() method both maps and flattens arrays by applying a transformation function to each element and then flattening the result into a new array by one level. It's ideal for situations where you need to both change and reduce the nesting of arrays simultaneously. The concat() method, on the other hand, combines arrays or values with an existing array into a new array without altering the originals, suitable for joining multiple arrays or adding additional elements. The choice between them depends on whether the task involves combining different structures or transforming nested arrays .

The push() method adds one or more elements to the end of an array, affecting the array's length and structure. It's an efficient choice when appending data sequentially. The unshift() method, however, adds one or more elements at the beginning of an array, shifting existing elements to higher indices. This accommodates scenarios requiring data insertion at the beginning of the sequence. Unshift is typically less performant due to moving array elements, but is essential when the element order dictates that new data should appear first .

The map() method applies a provided function to each element of the array, returning a new array of the transformed results. This operation does not change the original array. In contrast, the filter() method also iterates through each element but only returns those elements that satisfy a specified test condition, producing a new array with these elements. Both methods do not mutate the original array and require understanding of callback functions to effectively implement them .

Both slice() and splice() methods are used to handle subsets of arrays, but they operate in fundamentally different ways. slice() returns a new array containing the selected elements without modifying the original array, whereas splice() directly mutates the original array by removing or adding elements at specified indices. Slice is typically used when non-destructive copying is needed for further processing, while splice is suited for cases where you need to alter the structure of the existing array, such as implementing custom list management functions. Understanding when to preserve the original array versus modifying it is critical to selecting the appropriate method .

You might also like