JavaScript Array functions
Here’s a comprehensive list of commonly used JavaScript array methods:
1. Array Creation Methods:
Array.isArray()
: Checks if a value is an array.Array.of()
: Creates a new array instance from a variable number of arguments.Array.from()
: Creates a new array from an iterable or array-like object.
2. Array Manipulation Methods:
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 that element.shift()
: Removes the first element from an array and returns that element.unshift()
: Adds one or more elements to the beginning of an array and returns the new length.splice(start, deleteCount, item1, item2, ...)
: Adds/removes elements at a specified index.concat()
: Merges two or more arrays and returns a new array.slice(start, end)
: Returns a shallow copy of a portion of an array into a new array.
3. Array Iteration and Transformation Methods:
forEach(callback)
: Executes a provided function once for each array element.map(callback)
: Creates a new array populated with the results of calling a provided function on every element.filter(callback)
: Creates a new array with all elements that pass the test implemented by the provided function.reduce(callback, initialValue)
: Applies a function against an accumulator and each element of the array to reduce it to a single value.reduceRight(callback, initialValue)
: Similar toreduce()
, but works from right to left.find(callback)
: Returns the first element that satisfies the provided testing function.findIndex(callback)
: Returns the index of the first element that satisfies the provided testing function.some(callback)
: Tests whether at least one element in the array passes the provided function.every(callback)
: Tests whether all elements in the array pass the provided function.
4. Array Searching and Sorting Methods:
indexOf(searchElement, fromIndex)
: Returns the first index at which a given element can be found, or-1
if not found.lastIndexOf(searchElement, fromIndex)
: Returns the last index at which a given element can be found, or-1
if not found.includes(searchElement, fromIndex)
: Determines whether an array includes a certain element.sort(compareFunction)
: Sorts the elements of an array in place and returns the sorted array.reverse()
: Reverses the array elements in place.
5. Array Joining and Conversion Methods:
join(separator)
: Joins all elements of an array into a string, with an optional separator.toString()
: Returns a string representing the array and its elements.toLocaleString()
: Returns a localized string representing the array.
6. Array Filling and Copying Methods:
fill(value, start, end)
: Fills all elements of an array from a start index to an end index with a static value.copyWithin(target, start, end)
: Shallow copies part of an array to another location in the same array.
7. Array Methods for Set-Like Operations:
flat(depth)
: Flattens a nested array up to the specified depth.flatMap(callback)
: First maps each element using a mapping function, then flattens the result into a new array.entries()
: Returns a new array iterator object that contains the key/value pairs for each index.keys()
: Returns a new array iterator object that contains the keys for each index in the array.values()
: Returns a new array iterator object that contains the values for each index.from()
: Creates a new array from an array-like or iterable object.
8. Array Finding and Checking Methods:
find(callback)
: Returns the first element that satisfies the provided testing function.findIndex(callback)
: Returns the index of the first element that satisfies the provided testing function.some(callback)
: Tests whether at least one element in the array passes the provided test.every(callback)
: Tests whether all elements in the array pass the provided test.
This list covers most of the important array functions you will encounter in JavaScript. Each of these functions offers a powerful way to manipulate or inspect arrays in different ways, making them essential for working with data collections in JavaScript.