5 most useful array methods ( Javascript )
Tags: javascript, javascriptarrays, javascriptarraymethods, usefularraymethods
Published: 2021-01-30 10:53:46

By reading this post, you will become a master of this 5 awesome array methods which will make your life more easier.
<h2>Method - 1: map()</h2>
<p>This method is a kind of loop. You can loop through an array by using this method. It takes 3 parameters the first one is returning us the data/ value, the second one is the index number and the third parameter returns us the whole array.</p>
```javascript
const ara = [ 'Apple', 'Banana', 'Kiwi' ];
ara.map(( data, index, theArray ) => {
    console.log(data, index, theArray);
});
```
<br/>

<h2>Method - 2: forEach()</h2>
<p>This method is very similar to the method no -1. So this is also a kind of loop you can loop through an array with this method.</p>
```javascript
const ara = [ 'Apple', 'Banana', 'Kiwi' ];
ara.forEach(( data, index, theArray ) => {
    console.log(data, index, theArray);
});
```
<br/>
<h2>Difference between map() and forEach()</h2>
<p> forEach() ->  This iterates over a list and applies some operation with side effects to each list member (example: saving every list item to the database)</p>
<p> map() ->  This iterates over a list, transforms each member of that list, and returns another list of the same size with the transformed members (example: transforming list of strings to uppercase). It does not mutate the array on which it is called (although if passed a callback function, it may do so).</p>
<br/>

<h2>Method -3: filter()</h2>
<p>Well this method is my most favourite one. What is does, is it can filter through an array. Suppose you have an array. In this array you have put all the prices of something. Now you want the prices from the array which are only under 100. How can we do that? So in this case, we can use the filter() method. Let me show you an example.</p>
```javascript
const prices = [ 100, 200, 50, 30, 400, 50, 30, 450, 89, 90 ];
const under100 = prices.filter(( value, index, array ) => {
    return value < 100;
});
console.log(under100);
```
<p>Now the prices under 100 will be stored under a separate array and then you can use it by just calling the variable ``under100``. This method takes three parameters. As same as the other methods. The first one is The value, second one is the unique index number and the third one returns the whole array.</p>
<br/>

<h2>Method - 4: includes()</h2>
<p>This method is also a very useful one. Suppose you have an array and under that, you have put some names of people now you want to know that is the name - "Jack" is available under that array! So in this case you can use this awesome method.</p>
```javascript
const names = [ 'July', 'Jack', 'Lauv"];
const isItHere = names.includes('Jack');
console.log(isItHere);
```
<p>This method will return true or false/ Boolean value if it is containing the value under the array</p>
<br/>

<h2>Method - 5: reduce()</h2>
<p>This method is really really helpful. Suppose you have an array of numbers and Now you want to get the sum of the numbers in the array. So in this case, you can use this method. </p>
```javascript
const numbers = [ 12, 23, 34, 45, 5, 7 ];
const subtract = numbers.reduce(( value, index, array ) => {
    return value + index;
});
consolelog( subtract ); 
```
<p>You can also concat strings with this array method. It takes the same parameters as like the others. First one the value, second one is the index number and the third number is the whole array.</p>

<h3>So these are the top 5 javascript array methods. Thanks for reading this article. If you found any wrong informations here please let me know. And make sure you follow me to recieve all the informational posts just like that.</h3>
<p>:)</p>