Array Methods in JavaScript

Array Methods in JavaScript

Array

An array is a single variable, which holds more than one value. A pair of square brackets [ ] can be used to symbolize an array in JavaScript.

Syntax

const array_name = [Item1,Item2,Item3,....];

Example

const fruit = ['apple','mango','grapes','orange'];
console.log(fruit);

Output

['apple','mango','grapes','orange']

Alternate Way to create an Array

const array_name = new Array("item1","item2","item3");

Example

const fruit = new Array("apple","mango","orange","grapes");
console.log(fruit);

Output

["apple","mango","orange","grapes"]

Access the elements of an array

We can access the elements of an Array using an index number.

Indexing of elements begins from 0 in arrays means the first element index is zero.

Example

const fruit = new Array("apple","mango","orange","grapes");
console.log(fruit[0]);
console.log(fruit[1]);
console.log(fruit[2]);

Output

apple
mango
orange

Array Methods

1.push()

The push() method is used to add a new element to the array (at the end).

Example

const fruit = new Array("apple","mango","orange","grapes");
fruit.push("pineapple");
console.log(fruit);
['apple','mango','orange','grapes','pineapple']

2.pop()

The pop() method is used to remove the last element of an array.

Example

const fruit = new Array("apple","mango","orange","grapes");
fruit.pop();
console.log(fruit);
['apple','mango','orange']

3.slice()

The slice() method is used to select a part of an array and returns the new array. The method has two parameters:- start and end. It selects from a given start, up to a (not inclusive) given end.

Syntax

array.slice(start,end)

Example

const fruit = new Array("apple","mango","orange","grapes");
fruit.slice(1,3);
console.log(fruit);
["mango","orange"]

4.splice()

The splice() method is used to add or remove elements from the array. It overwrites the original array.

Syntax

array.splice(index, number, item1...itemx)

index specifies the position to add/remove elements.

number specifies the number of elements to be removed.

item1..itemx specifies the items to be added.

Example

const fruit = new Array("apple","mango","orange","grapes");
fruit.splice(1,1,"pineapple");
console.log(fruit);
["apple","pineapple","orange","grapes"]

5.concat()

The concat() method is used to join two or more arrays. It returns the new array containing the joined arrays. This method does not changes the existing arrays.

Example

const array1 = ["abc","xyz"]
const array2 = ["ghi","klm"]
const array3 = array1.concat(array2);
["abc","xyz","ghi","klm"]

6.fill()

The fill() method is used to fill the specified elements in an array with a value. It overwrites the original array.

Syntax

array.fill(value,start,end)

the value specifies the value to fill in.

start specifies the starting index.

end specifies the ending index.

Example

const fruit = new Array("apple","mango","orange","grapes");
fruit.fill("pineapple",2,4)
console.log(fruit)
["apple","mango","pineapple","pineapple"]

7.includes()

The includes() method returns true if an array contains a specified value. It returns false if the value is not found. This method is case-sensitive.

Syntax

array.includes(value,start)

value specifies the value to be looked for.

start specifies the starting index to look for the value.

Example

const fruit = new Array("apple","mango","orange","grapes");
console.log(fruit.includes("mango",2))
false

8.indexOf()

The indexOf() method returns the first index (position) of a specified value. It returns -1 if the value is not found. This method starts at a specified index and searches from left to right.

Syntax

array.indexOf(value,start)

value specifies the item to be searched.

start specifies the starting index.

Example

const fruits = ["Banana", "Orange", "Apple", "Mango", "Apple"];
console.log(fruits.indexOf("Apple", 3));
4

9.isArray()

The isArray() method returns true if an object is an array, otherwise false.

Syntax

Array.isArray(obj)

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(Array.isArray(fruits));
true

10.join()

The join() method returns an array as a string. This method does not change the original array. We can use any separator but the default is a comma(,).

Syntax

array.join(separator)

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.join(" and "));
Banana and Orange and Apple and Mango

11.map()

The map() creates a new array by calling a function for every array element. It calls a function once for each element in an array. It does not changes the original array.

Example

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

12.reverse()

The reverse() method reverses the order of the elements in an array. This method overwrites the original array.

Syntax

array.reverse()

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.reverse());
["Mango","Apple","Orange","Banana"]

13.shift()

The shift() method removes the first item of an array. This method changes the original array. It returns the shifted element.

Syntax

array.shift()

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.shift());
Banana

14.sort()

The sort() method sorts the elements of an array. It overwrites the original array. This method sorts the elements as strings in alphabetical and ascending order.

Syntax

array.sort()

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.sort());
["Apple","Banana","Mango","Orange"]

15.unshift()

The unshift() method adds new elements to the beginning of an array. This method overwrites the original array.

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.unshift("Lemon", "Pineapple"));
["Lemon","Pineapple","Banana","Orange","Apple","Mango"]

Well! That's quite a long article. If you enjoyed then do like and share, Thanks, and see you in the upcoming article. ✌️

Do not forget to share your valuable feedback.