Array Methods

What is an array?

Arrays are used to store multiple values in a single variable.

We denote the array by a square bracket[].

This is compared to a variable that can store only one value. Each item in an array has an index, that allows you to access it. In JavaScript, arrays start at index zero.

Syntax-

let x = ["hello"]

Array Methods-

1.Concatenation()

As his name suggests concatenation is used to merge two or more arrays. This method does not change the existing arrays but instead returns a new array.

let x = [55,78 ]
let y = [5,4]
console.log(x.concat(y));

2.Fill()

In this method, we can change elements in an array. This method does change the existing array to a static value and instead returns a new value.

Syntax-

let x = ["india","pakistan","nepal","bhutan"]
console.log(x.fill("kpk",1,3));

Here Fill with"kpk" from position 1 until position 3 but 3 is exclusive.

3.includes()

This method determines whether an array includes a certain value among its entries, returning true or false appropriately.

Syntax-

syntax-let x = ["india" , "pakistan", "nepal", "bhutan" ]
console.log(x.includes("india"));

4.indexOf()

This method returns the index at which a given element can be found in the array. if the value is not found we get -1.

Syntax-

let x = ["india" , "pakistan", "nepal", "bhutan" ]
console.log(x.indexOf("india"));

5.isArray()

This method determines whether the passed value is an array and returns true or false appropriately.

Syntax-

let x = ["india" , "pakistan", "nepal", "bhutan" ]
y = 5
console.log(Array.isArray(x));

6.join()

The method creates and returns a new string by concatenating all of the elements in an array. separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Syntax-

let x = ["india" , "pakistan", "nepal", "bhutan" ]
console.log(x.join("-"));

7.lastIndexOf()

The method returns the last index at which a given element can be found in the array, or -1 if it is not present.

Syntax-

let x = ["india" , "pakistan", "nepal", "bhutan","india" ]
console.log(x.lastIndexOf("india"));

8.map()

Whatever I am going to write inside the map will be linked with all the elements.

Syntax-

let x = [1,4,9,64 ]
console.log(x.map(Math.sqrt));

9.pop()

This method removes the last element from an array and returns that element. This method changes the length of the array.

Syntax-

let x = [1,4,9,64 ]
console.log(x.pop());
console.log(x);

10.shift()

This method removes the first element from an array and returns that removed element. This method changes the length of the array.

Syntax-

let x = [1,4,9,64 ]
console.log(x.shift());
console.log(x);

11.reverse()

This method reverses an array and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first.

Syntax-

let x = [1,4,9,64 ]
console.log(x.reverse());

12.sort()

This method sorts the elements of an array and returns to the same array, now sorted. The default sort order is ascending.

Syntax-

let x = ["zoo","dog","apple","boy",] 
console.log(x.sort());

13.unshift()

This method adds one or more elements to the beginning of an array and returns the new length of the array.

Syntax-

let x = ["zoo","dog","apple","boy",] 
console.log(x.unshift(5));
console.log(x)

14.toString()

This method returns a string representing the specified array and its elements.

15.splice()

This method changes the contents of an array by removing or replacing existing elements and adding new elements in place.

Syntax-

let x = ["zoo","dog","apple","boy",] 
console.log(x.splice(1,1,"cat"));
console.log(x);

16.slice()

This method returns a copy of a portion of an array into a new array object selected where the start and end represent the index of items in that array. The original array will not be modified.

Syntax-

let x = ["zoo","dog","apple","boy",] 
console.log(x.slice(1,3));

17.push()

This method adds one or more elements to the end of an array and returns the new length of the array.

Syntax-

let x = ["zoo","dog","apple","boy",] 
console.log(x.push(10));
console.log(x);

18.of()

The method creates a new array instance from a variable number of arguments, regardless of the number or type of the arguments.

Syntax-

let x = ('car')
console.log(Array.of(x));

19.values()

This method returns a new array iterator object that iterates the value of each item in the array.

20.keys()

This method returns a new Array Iterator object that contains the keys for each index in the array.