# Arrays In Javascript

# What Is An Array ?
An arrays is simply a data structure which helps to store collection of items or multiple values in a single variable. 
In other programming languages like C, C++ or Java. Array can store items which are of similar data type. But in JavaScript Array can store items with dissimilar data type in a same variable.

# Why we need an array?
Suppose you want to store the list of 10 fruits that you like. Assigning a single variable for a fruit is lengthy and it takes a lot of time and variable names. Instead we use array to store multiple values in a single variable. 
For example:-

```
// Storing list of 3 fruits name in an array
let fruitsILike = ["Apple", "Orange", "Banana"] ;
``` 

# Adding Items In An Array

**1.  Adding Items In The End**

You can use push() function or method to add items at the end of an array.

```
let numbers = [1,2,3];
numbers.push(4,5); // 4 and 5  will be added at the last
console.log(numbers);
``` 


 
**2.  Adding Items In The Beginning**

If you want to add items at the beginning of an array you use unshift() function.

```
let numbers = [1,2,3];
numbers.unshift(0,-1); // adds 0 and -1 at the beginning of an array
console.log(numbers);
``` 

**3. Adding Items In The Middle**

splice() method is used to add new elements in an array or remove elements from an array.
It has 3 parameters.
1st parameter is starting position.
2nd parameter is delete count.
3rd parameter lets you add items an array.

```
let months = ["Jan", "Feb", "May", "June"];
months.splice(2, 0, "March", "April"); // 2 is the position where we want to start from , 0 is the no of items we want to delete, "March" and "April" are items that you want to add
console.log(months); // Prints [ 'Jan', 'Feb', 'March', 'April', 'May', 'June' ]

``` 

# Deleting Items In An Array
To delete items in an array we have 3 methods - pop(), shift(), splice().

** 1. Deleting Items From End**

We use pop() method to delete last item in an array.

```
let fruits = ["Apple","Mango", "Orange"];
fruits.pop();
console.log(fruits); // prints [ 'Apple', 'Mango' ]
``` 

**2. Deleting From Front**

We use shift() method to delete items which are at first position.


```

let fruits = ["Apple","Mango", "Orange"];
fruits.shift();
console.log(fruits); // Prints [ 'Mango', 'Orange' ]
``` 

**3. Deleting From Middle**

We use splice() method to delete items from middle or any position or index that we like.
It has 2 parameters.
1st parameter is starting position.
2nd parameter is delete count.


```
let fruits = ["Apple", "Mango", "Orange", "Grapes", "Watermelon"];
fruits.splice(2, 1); // 2 is the starting index and 1 is the number of items to be deleted
console.log(fruits); // Output [ 'Apple', 'Mango', 'Grapes', 'Watermelon' ]
``` 

# Emptying An Array
There are multiple ways to empty an array, out of which we are going to look only 2 ways to empty an array.

```
// 1st method
let fruits = ["Apple","Mango", "Orange"];
fruits = [];
cosole.log(fruits); // here array will be empty

//2nd method
let ages = [20, 35, 90];
ages.length = 0;
console.log(ages); // Here array will be empty

``` 

# Array Concatenation
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

```
let fruits = ["Apple", "Mango", "Orange", "Grapes", "Watermelon"];
let ages = [20, 40, 80, 30];

let newArray = ages.concat(fruits); 
console.log(newArray);// Output = [ 20, 40, 80, 30, 'Apple', 'Mango', 'Orange', 'Grapes', 'Watermelon' ]
``` 












