JavaScript Array Methods

JavaScript Array Methods

javascript array

basically, an array is a collection of values. It is a list of items, that contains a list of elements which store multiple values under a single variable. where each item has a particular position in the list.JavaScript array can store mixed data formats in a single array.

Declaring an array

In general, there are two ways to declare an array. The syntax is as follows:

let array1 = [1, 4, 8, 20, 16, ...]; //  way 1
let array2 = new Array( 1, 2, 3, 4, 5); // way 2

Array methods

push():

To add a new one or more element at the end of an array, we can use the push() method. This method will modify your existing array.

let arr = [1, 6, 9, true,];
arr.push(100);
console.log(arr);
 // output- [ 1, 6, 9, true, 100 ]

pop():

This method removes the last element present in the array and returns that element. This method changes the length of the array.

let arr = [1, 6, 9, true, 100];
arr.pop();
console.log(arr);
// output- [ 1, 6, 9, true ]

unshift()

It is similar to how the push() method works, the unshift() method takes your array and adds one or more elements to the start of the array instead of the end. This method will also modify your existing array.

let arr = [1, 6, 9, true,];
arr.unshift(100);
console.log(arr);
// output-[ 100, 1, 6, 9, true ]

shift():

This method removes the first element present in the array and returns that element. This method changes the length of the array.

let arr = [1, 6, 9, true, 100];
arr.shift();
console.log(arr);
// output-[ 6, 9, true, 100 ]

concat():

By using this method can merge two or more arrays into a new array. It does not modify the existing arrays but creates a new one.

let arr1 = [1, 2, 3, 4];
let arr2 = [4, 5, 6, 7];

console.log(arr1.concat(arr2));
 // output [ 1, 2, 3, 4, 4, 5, 6, 7]

indexof():

This method is used to find the index of a particular element in an array. It aslo returns the numerical index or -1 if the element is not present in the array.

let arr = [1, 6, 9, true,];
console.log(arr.indexOf(1));
console.log(arr.indexOf(2));
//output-
//0
//-1

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.

let arr = [1, 6, 9, true, 1];
console.log(arr.astIndexOf(1));
//output- 4

includes():

This method is used to check whether the array contains the specified element or not. Depending upon whether that element is present or not, it will return true or false respectively.

let arr = [1, 6, 9, true, 100];
console.log(arr.includes(1));
console.log(arr.includes(2));
//output-
// true
// false

fill():

This method fills specified elements in an array with a value and overwrites the original array. The start and end position can be specified. If not, all elements will be filled.

syntax - array.fill{value, start(default 0), end(default array.length)}

let arr = [1, 6, 9, true, 100];
console.log(arr.fill(0, 2, 4));

// output -[ 1, 6, 0, 0, 100 ]

slice():

This method extracts the part of the given array and returns it. This method doesn't change the original array.

syntax- array.slice(start,end)

let arr = [1, 6, 9, true, 100];
console.log(arr.slice(1, 4));
//output-[ 6, 9, true ]

splice():

This method is used to add/remove the elements to/from the existing array. By specifying the index and number of elements to delete, it modifies the array.

syntax- array.splice(index, delete, item1, ....., itemX)

index-The position to add/remove items.
Negative value defines the position from the end of the array.

delete- The number of elements to be removed.

items- The elements to be inserted.

let arr = [1, 6, 9, true, 100, 23];
arr.splice(1, 2, 11, 22);
console.log(arr);
//output-[ 1, 11, 22, true, 100, 23 ]

reverse():

This method is used to reverse the order of elements in the array. It does not reverse the contents of the array but just the order itself.

let arr = [1, 6, 9, 328, 100];
console.log(arr.reverse());
//output-[ 100, 328, 9, 6, 1 ]

map():

This array method calls the specified function for every array element and returns the new array. This method doesn't change the original array.

let maths = [1, 4, 9, 16, 25];
console.log(maths.map(Math.sqrt));
//output-[ 1, 2, 3, 4, 5 ]