# 10 Array Methods in JavaScript

# Array Methods in JavaScript are Following:



## 1) **push()**
`push()` method is use to ** add element** at the **end of an Array**
### example

```
let car = ["alto", "wagonR", "swift", "eeco"];
car.push("ertiga");
console.log(car);

//output: [ 'alto', 'wagonR', 'swift', 'eeco', 'ertiga' ]
``` 



## 2) **pop()**
`pop()` method is use to remove element at the **end of an Array**

### example

```
let car = ["alto", "wagonR", "swift", "eeco"];
car.pop();
console.log(car);

//output: [ 'alto', 'wagonR', 'swift' ]
``` 


## 3) **toString()**
`toString()` method is use to get output of an **array as a string,** 
in short words `toString()` is use to change array into string

### example:
```
let car = ["alto", "wagonR", "swift", "eeco"];
let hold = car.toString();
console.log(hold);

//output: alto,wagonR,swift,eeco
``` 



## 4) **shift()**

`shift()` method is use to **remove element** at the **start of an Array.**

### example 1

```
let car = ["alto", "wagonR", "swift", "eeco"];
car.shift();
console.log(car);

//output: [ 'wagonR', 'swift', 'eeco' ]
```

but if we store it in variable it shift that element from that array, example:

### example 2

```
let car = ["alto", "wagonR", "swift", "eeco"];
let hold = car.shift();
console.log(hold);

// output: alto
```

## 5) **unshift()**

`unshift()` method is use to **add element** at the **start of an Array.**

### example

```
let car = ["alto", "wagonR", "swift", "eeco"];
car.unshift("ertiga");
console.log(car);

//output: [ 'ertiga', 'alto', 'wagonR', 'swift', 'eeco' ]
``` 

## 6) **concat()**

`concat()` method is use to **merge two arrays** 

### example


```
let car = ["alto", "wagonR", "swift"];
let bike = ["hayabusa", "duke", "himalayan"]
let hold = car.concat(bike)
console.log(hold)

//output: [ 'alto', 'wagonR', 'swift', 'hayabusa', 'duke', 'himalayan' ]
``` 

## 7) **sort()**

`sort()` method is use to **sort array elements alphabetically** 

### example 

```
let car = ["alto", "wagonR", "swift", "eeco"];
car.sort();
console.log(car);

//output: [ 'alto', 'eeco', 'swift', 'wagonR' ]
``` 

## 8) **reverse()**

`reverse()` method is use to **reverse the positions of array elements** 

### example

```
let car = ["alto", "wagonR", "swift", "eeco"];
car.reverse();
console.log(car);

//output: [ 'eeco', 'swift', 'wagonR', 'alto' ]
``` 

## 9) **slice()**

`slice()` method is use to **get elements from the array as per position given** 

### example 1

in below example meaning of `slice(3)` is, from that array all the elements from 3rd position will be thrown to the output (including 3rd position also)

```
let car = ["alto", "wagonR", "swift", "eeco", "baleno", "brezza"];
let hold = car.slice(3);

console.log(hold);

//output: [ 'eeco', 'baleno', 'brezza' ]
``` 

### example 2

in below example meaning of `slice(2,4)` is, all the elements from 2nd position to 4th position will be thrown as an output (starting position is included, ending position is always excluded)



```
let car = ["alto", "wagonR", "swift", "eeco", "baleno", "brezza"];
let hold = car.slice(2,4);

console.log(hold);

//output: [ 'swift', 'eeco' ]
``` 

## 10) **splice()**

`splice()` method is use to remove elements as per the position given

### example 1

in below example meaning of `splice(3,2)` is, 3 is  starting index and 2 is number of elements you want to delete from starting index.

so here, 2 elements from 3rd positions are removed.

starting position is always included.

```
let car = ["alto", "wagonR", "swift", "eeco", "baleno", "brezza"];
car.splice(3,2);

console.log(car);

//output: [ 'alto', 'wagonR', 'swift', 'brezza' ]
```

### example 2

in below example meaning of `splice(3,2,"bmw","audi")` is, 

3 is starting index and 2 is number of elements you want to delete from starting index.

 "bmw" and "audi" is items that will be replace deleted elements.


```
let car = ["alto", "wagonR", "swift", "eeco", "baleno", "brezza"];
car.splice(3,2, 'bmw', 'audi');

//output: [ 'alto', 'wagonR', 'swift', 'bmw', 'audi', 'brezza' ]

``` 

