jsguides

Reference

Array Methods

Methods available on every JavaScript Array instance.

  1. Array.from()

    Array.from() creates a new Array from any iterable or array-like value, with optional mapFn and thisArg for inline transformation during conversion.

  2. Array.fromAsync()

    Array.fromAsync() creates a Promise that resolves to a new array from an async iterable, iterable, or array-like input.

  3. Array.prototype.at()

    Array.prototype.at() accesses array elements by positive or negative index, counting from the end when negative. Returns undefined when out of bounds.

  4. Array.prototype.concat()

    Array.prototype.concat() merges arrays without mutation. Learn syntax, parameters, and practical patterns for concatenating arrays and values in JavaScript.

  5. Array.prototype.copyWithin()

    copyWithin() shifts array elements to another index in place. Covers syntax, negative indices, rotation, and overwrite behavior with no new allocation.

  6. Array.prototype.entries()

    Array.prototype.entries() returns a new iterator yielding [index, value] pairs for each element, making index-aware iteration straightforward in for...of loops.

  7. Array.prototype.every()

    Learn Array.prototype.every(): tests if all elements pass a condition, returns a boolean, and short-circuits on the first failing element.

  8. Array.prototype.fill()

    Array.prototype.fill() replaces array elements with a static value. Covers negative indices, the object reference trap, and typed array support.

  9. Array.prototype.filter()

    Array.prototype.filter() returns a new array of elements that pass a callback test. Use it to filter by condition, remove falsy values, and deduplicate.

  10. Array.prototype.find()

    Array.prototype.find() returns the first array element that satisfies a testing function. Returns undefined when no element matches the condition.

  11. Array.prototype.findIndex()

    Array.prototype.findIndex() returns the index of the first array element that passes a test, or -1 if none do. Covers patterns and how it differs from find().

  12. Array.prototype.findLast()

    Array.prototype.findLast() searches an array from the end and returns the last element that matches a testing function, or undefined.

  13. Array.prototype.findLastIndex()

    Array.prototype.findLastIndex() searches an array in reverse and returns the index of the last element that matches a testing function, or -1 if none matches.

  14. Array.prototype.flat()

    Array.prototype.flat() flattens nested arrays to a specified depth, returning a new array with sub-array elements concatenated into one level.

  15. Array.prototype.flatMap()

    Array.prototype.flatMap() combines map and flatten in one pass. Covers syntax, filter-and-map patterns, depth limits, performance over chaining map().flat().

  16. Array.prototype.forEach()

    Array.prototype.forEach() runs a callback for every array element. Returns undefined, cannot break early, and does not await async callbacks.

  17. Array.prototype.from()

    Array.prototype.from() creates a shallow-copied array from array-like or iterable objects. Accepts a map function to transform elements during conversion.

  18. Array.prototype.includes()

    Array.prototype.includes() checks if an array contains a value, returning a boolean. Handles NaN correctly and accepts an optional fromIndex.

  19. Array.prototype.indexOf()

    Array.prototype.indexOf() finds the first position of a value in an array, returning the index or -1 when the element is not present.

  20. Array.prototype.isArray()

    Use Array.isArray() to reliably detect array values in JavaScript. Handles cross-realm arrays where instanceof fails, with safe type guards for API validation.

  21. Array.prototype.join()

    Array.prototype.join() concatenates all array elements into a single string with an optional custom separator. Returns an empty string for empty arrays.

  22. Array.prototype.keys()

    Array.prototype.keys() returns an iterator that yields each index of an array, making it straightforward to iterate positions in for...of loops.

  23. Array.prototype.lastIndexOf()

    Array.prototype.lastIndexOf() searches an array from right to left and returns the last index where a given element appears, or -1 if it is not found.

  24. Array.prototype.map()

    Array.prototype.map() creates a new array by running a function on every element and collecting the results. Transform data without mutating the original.

  25. Array.prototype.of()

    Array.of() creates a new Array from variadic arguments, treating each as an element — avoiding the number-as-length gotcha in the Array constructor.

  26. Array.prototype.pop()

    Array.prototype.pop() removes the last element from an array and returns it. An O(1) mutating method for stacks, undo systems, and LIFO data structures.

  27. Array.prototype.push()

    Array.prototype.push() appends elements to the end of an array, returning the new length. Covers syntax, patterns, performance, and immutable alternatives.

  28. Array.prototype.reduce()

    Array.prototype.reduce() accumulates array elements into a single value via a callback — ideal for summing, grouping, and data transformation.

  29. Array.prototype.reduceRight()

    Array.prototype.reduceRight() reduces an array from right to left into a single value — ideal for function composition and right-associative operations.

  30. Array.prototype.reverse()

    Reverses an array in place and returns the same array reference. For an immutable reversed copy, use toReversed() instead.

  31. Array.prototype.shift()

    Array.prototype.shift() removes and returns the first element of an array. Covers queue patterns, performance tradeoffs, and shift() vs pop().

  32. Array.prototype.some()

    Array.prototype.some() tests whether at least one array element passes a callback. Returns true on the first match, false if no element passes.

  33. Array.prototype.sort()

    Sorts the elements of an array in place and returns the same array reference. Pass a compare function for numeric or custom order.

  34. Array.prototype.splice()

    Array.prototype.splice() adds, removes, or replaces array elements at any position and returns deleted elements. Covers patterns and toSpliced().

  35. Array.prototype.toReversed()

    Array.prototype.toReversed() returns a new array with elements in reversed order, leaving the original unchanged. Part of the ES2023 immutable array methods.

  36. Array.prototype.toSorted()

    Array.prototype.toSorted() returns a new sorted array without mutating the original. The immutable counterpart to sort(), introduced in ES2023.

  37. Array.prototype.toSpliced()

    Array.prototype.toSpliced() returns a new array with elements removed, inserted, or replaced at a given index, leaving the original unchanged.

  38. Array.prototype.toString()

    Array.prototype.toString() returns a comma-separated string of array elements calling toString() on each. Covers implicit coercion, null handling, and join().

  39. Array.prototype.unshift()

    Array.prototype.unshift() prepends elements to the beginning of an array and returns the new length. Covers patterns, performance, and immutable alternatives.

  40. Array.prototype.values()

    Array.prototype.values() returns a new Array Iterator that yields values for each index. Use it with for...of loops, spread syntax, and iterator patterns.

  41. Array.prototype.with()

    Array.prototype.with() replaces the element at a given index in a new array, preserving the original. Part of ES2023's immutable array methods.

  42. Object.groupBy()

    Object.groupBy() groups iterable elements into a null-prototype object, keyed by the string or symbol returned from a callback.