# Methods of Array:

# List of Array methods:

- length,
- push,
- slice,
- splice,
- split,
- concat,
- copyWithin,
- includes,
- index of,
- reverse,
- shift,
- unshift,
- sort,

### points to be remembered :
Methods that can modify the original array are,

- shift
- unshift
- split
- push
- slice
- splice
- copyWithin


## Lenght of Array (arr.length):

The length method in an array is used to calculate the number of elements present inside the array. 
** Syntax:**

 console.log**("name of array".length) **
```
let arr = ["abram", "john", "dustin", "steve", "professor"]
console.log**(arr.length)**;
 Output -> 5

let arr = ["abram", "john", "dustin", "steve", "professor"]
console.log**(arr[1])**;
 Output -> John     (arr[1] -> this denote the 1st index of the error)

let arr = ["abram", "john", "dustin", "steve", "professor"]
console.log**(arr[arr.length -1])**;      (length -1 => 5 - 1 = 4 )  
Output -> professor                      ( index of professor is 4)        
```
To change the values in an Array:

```
let arr = ["abram", "john", "dustin", "steve", "professor"]
arr[3] = "George"
console.log(arr);
 Output -> In index 3 steve will be replaced by George.
```
# Array Constructors:
Another way to declare an array is Array Constructor, this method is scarcely used by the developer in tech companies.
```
let newarr = newArr("abram", "john", "dustin", "steve", "professor")
```
# Push:
The push method in the array will modify the original array.
```
let arr =["abc", "def", "ghi" "jkl"]
console.log**(arr.push("mno"))**  Output -> 5

after pushing "mno" into the array the length will become 5,
if we again 
console.log(arr)  Output -> ['abc', 'def', 'ghi', 'jkl', 'mno']
```
# Slice:
In normal words, ** slice ** means ** cut **, like that in the array method slice will the elements in the array.

** Syntax: ** 

console.log**(arr.slice(start index, endIndex)**
The endIndex is explicit(not to be counted)
```
let arr = ["a", "b", "c", "d", "e", "f"]
console.log**(arr.slice(1, 4))**;
 Output -> ['b', 'c', 'd']
```
# Splice:
The splice method is quite similar to the push method. In splice, we can command the values at which index it to insert, it will also overwrite the original array.

** Syntax: **

 console.log**(arr.splice(inserting index, Number of items to be removed, value to be added)**
```
let arr = ["a", "b", "c", "d"]
console.log(arr.splice(2, 0, "new value1", "new value2")); 
 Output -> [ ]    it returns like because we didn't delete any element in the array
console.log(arr);
 Output -> ['a', 'b', 'new value 1', 'new value 2', 'c', 'd']
```
```
let arr = ["a", "b", "c", "d", "e"]
console.log(arr.splice(2, 2, "new value1", "new value2")); 
 Output -> [ 'c', 'd' ]    the reason for this output is we wrote 2 elements to be removed, so the 'c' & 'd' get removed.
console.log(arr);
 Output ->['a', 'b', 'new value1', new value2', 'e']
```

# Concate:
How to add two arrays in javascript? The answer to this question is **Concatenation**. By concat method, we are able to add two arrays.

**Syntax:**

console.log**(arr.concat(arr1,arr2))**;
```
let arr   = ["ab", "cd", "ef", "gh"]
let arr1 = [ 1, 2, 3, 4, 5, 6]

console.log(arr.concat(arr1));

let arr   = ["ab", "cd", "ef", "gh"]
let arr1 = [ 1, 2, 3, 4, 5, 6]
let arr2 = [7, 8, 9]

console.log(arr.concat(arr1, arr2))

[ we can also join more than one array at a time using the concat method]
```
# CopyWithin : 
The copyWithin() method copies array elements to another position in the array. This method overwrites the existing values.

**Syntax:**

console.log**(arr.copyWithin(startindex,replace value from index, to index))**
```
let arr = [1,2,3,4,5,6,7,8]
console.log(arr.copyWithin(1, 5, 7));
 Output -> [ 1, 6, 7, 4, 5, 6, 7, 8]

```

# Inclues:
This includes work like a boolean whether if the value is present it returns false other than it becomes false.

**Syntax:**

console.log**(arr.inclues(value to be checked, value to be in the index)**

```
let arr = [1, 2, 3, 4, 5, 6, 7]
console.log(arr.includes(5 , 4));
 Output -> true;

let arr = [1, 2, 3, 4, 5, 6, 7]
console.log(arr.includes(5 , 5));
 Output -> false;
```
# Index of:
This method is used to display the index of the element present in the array.

**Syntax:**

console.log**(arr.indexOf(3));**
```
let arr = [ 1, 2, 3, 4, 5]
console.log(arr.indexOf(3))
 Output -> 2
```
# Map: 
The method **map**  will be applied to all the elements present in the array. For example,
**Syntax:**

console.log**(arr.map(expression));**
```
let arr = [1, 4, 9, 16]
console.log(arr.map(Math.sqrt));
 Output -> [1, 2, 3, 4]     ( Math.sqrt -> this math function is used to find square root of the numbers)
```
# Reverse:
The reverse method will not modify the original array, it will only reverse the array format.

**Syntax:**

console.log**(arr.reverse())**
```
let arr = [ 1, 4, 9, 16 ]
console.log(arr.reverse());
```
# Shift:
This will modify the original array. **The shift() method removes the first item of an array.**

**Syntax:**

console.log**(arr.shift())**
```
let arr = [1, 4, 9, 16]
console.log(arr.shift());
 Output -> 1     [ the first element in the array gets removed ]
console.log(arr)
 Output -> [ 4, 9, 16 ]
```
# Sort: 
The sort() sorts the elements of an array. It will overwrite the original array. It sorts the elements as strings in alphabetical and ascending order.

**Syntax:**
console.log**(arr.sort())**
```
let names = ["abram", "paul", "john", "catherine", "michael"]
console.log(names.sort());
 Output -> ['abram', 'catherine', 'john', 'michael', 'paul']
```
#toString:
The toString() method returns a string with array values separated by commas. This method does not change the original array.

**Syntax:**
console.log**(arr.toString())**
```
let names = ["abram", "paul", "john", "catherine", "michael"]
console.log(names.toString());
 Output -> abram, paul, john, catherine, michael
```
# unshift:
The unshift() method adds new elements to the **beginning** of an array. This method overwrites the original array.
```
let names = ["abram", "paul", "john", "catherine", "michael"]
console.log(names.unshift("steve"));
 Output -> 6
console.log(names);
 Output -> ['steve', 'abram', 'paul', 'john', 'catherine', 'michael']
```
# Split:
The split() method splits a string into an array of substrings. This method returns the new array and it does not change the original string. If (" ") is used as a separator, the string is split between words.

**Syntax:**

console.log**(string.split(separator, limit))**

```
let str = "Javascript";
let ar1 = str.split("a");
console.log(ar1);
 Output -> ['J', 'v', 'script']
```
```
let str = "How old are you?";
const myArray = text.split(" ", 3);   [ 3 is the limit of the new array]
 Output -> ['how', 'old', 'are']
```

 
