Hey developers, welcome to the "Top 10 Array Methods of JavaScript" post. In this post, we are going to cover the Top 10 JavaScript Array methods with examples which are mostly used. This can be your JavaScript Array Methods Cheatsheet🔥.
So let's start without wasting any time😜.
JavaScript Array Methods with Example
i) filter()
This array method is used to create a new filtered array on the given conditions. The filter method executes the function for each element of the array.
Example:
ii) forEach()
This method is used to call a function for each element of the given array. But this method will not execute on the empty elements. This method is used to iterate over the elements of an array and it executes the given function for each element.
Example:
iii) includes()
This array method is used to find does the given array contains a specific value or not. It returns true if the array contains specified values else it will return false.
Example:
iv) map()
This javascript array method is used to iterate over an array. It calls the function for every element of an array. And it creates a new array by calling a specific function for every array element.
Example:
v) reduce()
This array method is used to perform some operations on the array to get a single value. It is an iterative method.
Example:
vi) sort()
This javascript array method is used to sort the given array. This array method compares two values and returns the same array as the reference which is now a sorted array.
Example:
vii) find()
This array method returns the first element in an array that passes a given test function. This array method returns 'undefined' if no element of the array pass the test functions. And it returns a single value as an output.
Example:
viii) findIndex()
This javascript array method is used to find the first index of the element in an array which pass a given test function. It returns single value output. This array method executes the function for each element of the array.
Example:
ix) reverse()
This array method is used to reverse the given array. It gives the same array but in this, the first element of the array becomes last and the last element of the array becomes first. and like this other elements of the arrays turned in the opposite direction.
Example:
x) join()
This javascript array method is used to join all the elements of the array into a string. And it separates the array element with a comma(,). It will not change the original and not create a new array. It returns the given array as a string which is by default separated by a comma (,).