In this article, we’ll learn about the array methods in JavaScript with some examples. Before we start, let’s look at some methods of the Array
class.
Contents
- Array()
- .from()
- .isArray()
- .at()
- .concat()
- .copyWithin()
- .every()
- .fill()
- .filter()
- .find()
- .findIndex()
- .flat()
- .forEach()
- .includes()
- .indexOf()
- .join()
- .lastIndexOf()
- .map()
- .pop()
- .shift()
- .push()
- .unshift()
- .reduce()
- .reduceRight()
- .reverse()
- .slice()
- .some()
- .sort()
- .splice()
- .toString()
- .values()
Array()
Accepts 1 integer argument and returns an array with those elements.
.from()
We use this method to create an array. It accepts two arguments, the first one accepts an array or some iterable object.
The second argument is for the mapping function (scroll below to learn more about map), it is optional.
.isArray()
This method accepts 1 argument and returns if it’s an array.
Now, we’ll go to the main part of this article.
.at()
Returns the element of the array of the given position. You can use negative numbers instead of doing array[array.length-1]
.
.concat()
Merges all the arguments to one array.
.copyWithin()
Copies the array within a range. Accepts 3 arguments.
.every()
Accepts a test function and returns a boolean if all the elements of the array pass the test.
.fill()
Fills the array with a given value. Accepts 3 arguments. 1st argument is the value we need to fill. The 2nd argument is the starting index. The last argument is the ending index. The last two parameters are optional.
.filter()
Accepts a test function to loop each array element and remove that element if failed the test.
.find()
Again, this accepts a test function and returns the element that the test function passed.
.findIndex()
Does the same thing as .find()
. Instead of returning the value, returns the position of the element in the array.
.flat()
Flats the array to a given depth. The depth will be 1 if no value.
.forEach()
Accepts an argument that will call for each element of the array
.includes()
Returns if the array contain the given value. Also, this is case-sensitive.
.indexOf()
Returns the least index of an element of the array (if found) after searching the value. Returns -1 if not found. The 2nd argument specifies the position to start the search.
.join()
Joins all the array elements into a string with a separator (if given).
.lastIndexOf()
Does the same thing as .indexOf
. Instead of giving the least index, this gives you the largest index.
.map()
Loops for each element of the array and executes the given function and change the value to the output of the function.
.pop()
Removes the last element of the array and returns it. (You may know stack operations)
.shift()
Removes the first element and return that element.
.push()
Push the given value to the array. Then, return the new length of the array.
.unshift()
Does the same as .push
but adds the elements to the first.
.reduce()
Accepts a reducer function as the first argument and executes it. If you can’t understand that, see below. The second arguments show the initial value, and it’s optional.
To calculate the sum of an array, we can do this.
We have a variable named initialValue
, that will be the initial value of the accumulator variable (or we can say the returned value). This will be passed to the reducer function. As the reducer function starts to execute, the value of the first parameter acc
is the value of the initialValue
, and the value of val
is the first element of the array. When the reducer function is executed, the value of acc
is the value it returned. Then the val
parameter will be the second element of the array and it keeps going until the last element of the array.
If the initial value is not given, the reducer function will start from the second element of the array, and the first element will be the initial value.
.reduceRight()
Does the same thing as .reduce
but works from right to left.
.reverse()
Reverses the array.
.slice()
Returns a shallow copy of an array within the range of the given start and end arguments. (The end argument is exclusive). If no arguments are given, it will copy the whole array.
.some()
Accepts a function and checks if some of the elements passed the test.
.sort()
Accepts a sorting function and sorts the array.
.splice()
Changes the content of the array by removing or replacing the elements. Accepts any number of arguments. The first argument is the starting position of the array. The second one is the delete count (how many elements to be removed). Then, the rest of the arguments are the elements to be added to the array.
.toString()
Returns the string of the elements.
.values()
Returns an array iterator that contains the values of each index.
Now, we know about the built-in methods of the Array
class. Some array methods are very similar to those methods that we can use with other classes. For example, .find()
, .findIndex()
, and .some()
are the same as in String
class.
Thanks for reading! If you have any questions or suggestions, leave a comment below.