Useful_JavaScript_Array_Methods
Useful_JavaScript_Array_Methods
JavaScript provides many useful methods to work with arrays. This guide covers the most
Adding/Removing Elements
- push(): Adds one or more elements to the end of an array and returns the new length.
- pop(): Removes the last element from an array and returns it.
- shift(): Removes the first element from an array and returns it.
- unshift(): Adds one or more elements to the beginning of an array and returns the new length.
Searching
- find(): Returns the first element that satisfies a condition.
- findIndex(): Returns the index of the first element that satisfies a condition.
- indexOf(): Returns the index of a specified element in the array, or -1 if not found.
Iteration
- forEach(): Executes a provided function once for each array element.
- map(): Creates a new array with the results of calling a function on every element.
- reduce(): Executes a reducer function on each element to reduce it to a single value.
- reduceRight(): Same as reduce(), but processes the array from right to left.
Array Transformation
- slice(): Returns a shallow copy of a portion of an array.
- flatMap(): Maps each element and flattens the result into a new array.