All about javascript array methods..
Tags: javascript, array, 100dayofcode, arraymethods
Published: 2020-05-19 15:16:24


# Javascript all Array method
Array is very useful in almost all programming language, here we see javascript array method whom we used mostly so let's get started.

### Array.join()
The Array<b>.join()</b> is used to converting whole array array to string & return a single string, if we use <b>.join()</b> method without any arguments then they separate element with ','
```javascript
let arr = [1,2,3];          // Init an array
arr.join();                 // return => "1,2,3"
arr.join(" ");              // return => "1 2 3"
arr.join("");               // return => "123"

let arr1 = new Array(10);   // Init empty array with length 10
arr1.join('_');             // return => "__________"
```
### Array.reverse()
The Array<b>.reverse()</b> method reverse the order of the elements of an array and return a reversed array.It's didn't take any arguments.
```javascript
var a = [1,2,3];           // Init an array
a.reverse();               // return => [3,2,1]
```
> Stings is also a part of an array.

### Array.sort()
The Array<b>.sort()</b> method sort the element of an array. If we apply short method without any arguments, they sort ascending order. also this can take a function in form of arguments where we provide return value. If arguments is presents so they sort according to argument provided by user.

```javascript
var a = [555,44,188,3];       // Init an array
a.sort()                      // now a is sorted in alphabetical order => [188,3,44,555]

// sort in descending order 
a.sort( (a,b) => a > b ? -1 : 1 );         
/* arguments in sort method is known as compareFunction 
there have firstEl & secondEl is parameters. this sort 
method sort like this [555,188,44,33] */

// sort in ascending order
a = ["ujjwal","devendar","roshan"];
a.sort( (a,b) => a > b ? -1 : 1);
/* sort => ["devendar","roshan","ujjwal"] */
```

### Array.concat()
The Array<b>.concat()</b> method return combined array of original and passed by arguments. concat() method does not modify the original array.
```javascript
let a = [1,2,3];              // Init an array
a.concat(4,6);                // return => [1,2,3,4,6]
a.concat([4,6]);              // return => [1,2,3,4,6]
a.concat([1,7],[7,2]);        // return => [1,2,3,1,7,7,2]
a.concat(4,[5,[7,2]]);        // return => [1,2,3,4,5,[7,2]]
```

### Array.slice()
The Array<b>.slice()</b> method can take max two arguments and return a part of an array. It's didn't change original array value.If we don't provide any arguments then return exact same array.
```javascript
let a = [1,2,3,4,5,6];             // Init an array
a.slice();                         // return same array => [1,2,3,4,5,6]
a.slice(0,4);                      // return => [1,2,3,4]
a.slice(4);                        // return => [5,6]
a.slice(1,-1);                     // return => [2,3,4,5]
a.slice(-4,-2);                    // return => [3,4]
```
### Array.splice()
The Array.<b>.splice()</b> method unlike <b>slice()</b>,<b>concat</b> can modify the original variable and return an array. If there have single argument they take as how many element you want to splice the array, & If there have two arguments so take as index value.
```javascript
let a = [1,2,3,4,5,6,7,8,9,10];       // Init an array
a.splice(4);                          // return => [5,6,7,8,9,10] && a = [1,2,3,4]   
a.splice(1,2);                        // return => [2,3] && a = [1,4]  
a.splice(1,1);                        // return => [4] && a = [1] 
```
### Array.push()
The Array<b>.push()</b> method is used to adding elements into array modify array and return length of array
```javascript
let a = [1,2];           // Init an array
a.push(5,3);             // a is now => [1,2,5,3] && return => 4
```
### Array.pop()
The Array<b>.pop()</b> method is used to remove last element of array and return last element
```javascript
let a = [1,2,3,4,5];      // Init an array
a.pop()                   // a is now => [1,2,3,4] && return 5
```
### Array.unshift()
The Array<b>.unshift()</b> is just like <b>Array.push()</b> method but this can insert elements from beginning, & like push() it's also return length of array
```javascript
let a = [1];              // Init an array
a.unshift(14)             // a is now => [14,1] && return => 2
a.unshift([45,15],2)      // a is now => [[45,15],2,14,1] && return => 4
```
### Array.shift()
The Array<b>.shift()</b> is just like <b>Array.pop()</> method but delete element from beginning, & just like pop() it's also return deleted elements
```javascript
let a = [1,2,3,4,5];        // Init an array
a.shift()                   // a is now => [2,3,4,5] && return 1
```
### Array.toString()
The Array<b>.toString()</b> is used to convert array to string and separate element with ',' but not include '[' & ']' in string.
```javascript
[1,2,3].toString()           // return => "1,2,3"
["a","b","c"].toString()     // return => "a,b,c"
[1,[2,'c']].toString()       // return => "1,2,c"
```
> Array.toLocalString() is localized version of toString().

### Array.forEach()
The Array<b>.forEach()</b> is used to invoking a function to specify for each element of an array which take a function with one arguments that is targeted element.

```javascript
let arr = ["one","two",3];
arr.forEach(
     (elem)=>{
        console.log(elem);
     }
)

/* output of program is 
"one"
"two"
3
*/
```
### Array.map()
The Array<b>.map()</b> is method of Array object with take a function, where have a arguments which target each elements same as <b>.forEach()</b> method & return a new modified array.
```javascript
let a = [1,2,3];
b = a.map(
     function(x){
           return x+2  
      }
)
//now output b is:
//[3,4,5]
``` 
### Array.filter()
The Array<b>.filter()</b> is to filtering data into array. the arguments of this function is same as .map() .forEach(), which take a function with boolean return. if function return true then elements are included in array.
```javascript
let arr = [1,2,3,4,5,6,7,8,9,10];
let b = arr.filter(
    function(x){
           return x/2 == 0;        
       }
)
// now b is =>
//[2,4,6,8,10] 
```
### Array.every()
The Array<b>.every()</b> is used to asking question about array is every elements is true when we apply given condition, & return boolean value (true,false)
```javascript
let arr = [1,2,345,6,15,48];
let b = arr.every(
     function(x){
           return x < 50;
       }
)
// now b is => false

b = arr.every(
     function(x){
           return x < 500;
       }
)
// now b is => true
```
### Array.some()
The Array<b>.some()</b> is used to asking question about array is any elements is true when we apply given condition, & return boolean value (true,false)
```javascript
let arr = [8,2,345,6,15,48];
let b = arr.some(
     function(x){
           return x < 50;
       }
)
// now b is => true

b = arr.some(
     function(x){
           return x < 2;
       }
)
// now b is => true
```
###