Array Methods in JavaScript

Array Methods in JavaScript

Array is most important in javascript and in that article i am writing about array and it's different methods.

What is Array

Array Methods

length

push

slice

splice

concatenation

fill

includes

indexOf

isArray()

join

lastindexOf

map

pop

shift

sort

unshift

converting to array

What is Array

An array is an object that can store multiple values in one variable. The array can be represented by a square bracket[]. All the elements in the array can be separated by a comma(,). And the indexing of the array always starts from 0.

Syntax:

let array[1, 2, 3];

Example:

let names = ['John', 'Max', 'Sam'];
console.log(names);

Output:

['John', 'Max', 'Sam']

Array Methods

Array methods are functions built-in into JavaScript that we can apply to our arrays Each method has a unique function that performs a change or calculation to our array and saves us from writing common functions.

There are a lot of array methods to use its function.

length

push

slice

splice

concatenation

fill

includes

indexOf

isArray()

join

lastindexOf

map

pop

shift

sort

unshift

converting to array

length

The length method defines the number of elements in that array.

Syntax:

array.length

Example:

const fruits = ['Apple', 'Banana', 'Grapes', 'Oranges'];
console.log(fruits.length);

Output:

4

In the above example, the length of array elements is 4.

Push

Using the push method you can insert new values inside the array at the end.

Syntax:

array.push(element1,element2,....,elementn);

Example:

let cars = ['Volvo', 'BMW', 'Swift', 'Baleno'];
cars.push('Duster');
console.log(cars);

Output:

[ 'Volvo', 'BMW', 'Swift', 'Baleno', 'Duster' ]

In the above example using the push() method Duster name is added to the array at the end.

Slice

In the slice method, the last value is not included in the array.

Syntax:

arr.slice([start], [end])

Example:

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2, 4));

Output:

['camel', 'duck']

In the above example, at indexing point 2 camel is there and at indexing 4 elephant is there but in the slice, the last value is not included that's why duck is printed in the output.

2 - start inserting from this index

4 - end index

Splice

To insert value inside an array.

Syntax:

arr.splice([start], [end])

Example:

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);

Output:

['Jan', 'Feb', 'March', 'April', 'June']

In the above example, the Feb is added at 1 indexing position in the array.

Concatenation

It can connect two or more arrays.

Syntax:

array1.conact(array2)

Example:

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);

Output:

[ 'a', 'b', 'c', 'd', 'e', 'f' ]

In the above example, array1 and array2 are connected.

fill

Using the fill method you can fill the elements in an array.

Syntax:

arr.fill(value, start, end)

Example:

let array4 = [1, 2, 3, 4];
console.log(array4.fill(0, 2, 4));

Output:

[1, 2, 0, 0]

In the above example fill 0 from position 2 to position 4.

includes

The include method returns true if the specified element is there in the array otherwise it returns false.

Syntax:

array.includes(value)

Example:

let array1 = ['cat', 'dog', 'cow'];
console.log(array1.includes('cat'));

Output:

true

In the above example, the 'cat' is included in that array that's why the output will give true value.

indexOf

It can define the index of a given element of an array. If it is not present in that array then it will give a -1 value.

Syntax:

indexOf(value)

Example:

let array2 = ['Alex', 1, 2, 3];
console.log(indexOf('Alex'));

Output:

0

In the above example, Alex is given at 0 indexing.

isArray

It returns true if an element is an array otherwise it returns false. It can check only is there the value passed is in an array or not.

Syntax:

array.isArray(value)

Example:

let array5 = [4, 5, 6, 7];
console.log(Array.isArray(5));

Output:

true

In the above example, the 5 is in that array that's why the output will give a true value.

join

It joins the elements of an array as a string. And it is separated by a comma or separate string.

Syntax:

join()

Example:

let array6 = [1, 2, 3, 4];
let names = array6.join(' 5 ');
console.log(var);

Output:

1 5 2 5 3 5 4

In the above example, the 5 can join every element of a given array.

lastindexOf

It searches the specified element in an array and returns the index of the last match.

Syntax:

array.lastIndexOf(element,index)

Example:

const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
console.log(animals.lastIndexOf('Dodo'));

Output:

3

In the above example, 'Dodo's name's last index is 3 that's why it will give output 3.

map

It creates a new array and it can call the functions.

Syntax:

array.map(callback(element ,index))

Example:

const maths = [1, 4, 9, 16];
console.log = (maths.map(Math.sqrt));

Output:

[1, 2, 3, 4]

In the above example math.sqrt function call and it will give the square root of these elements.

pop

It removes the last element from an array.

Syntax:

pop()

Example:

let array8 = [8, 9, 10, 11, 12];
console.log(array8.pop());
console.log(array8);

Output:

[8, 9, 10, 11]

In the above example, the 12 is removed from that array by using the pop method.

reverse

It reverses the elements of the given array.

Syntax:

reverse()

Example:

let array9 = [1, 2, 3, 4, 5];
console.log(array9.reverse());

Output:

[5, 4, 3, 2, 1]

In the above example, the array elements will give in reverse order.

shift

It removes the first element from an array.

Syntax:

shift()

Example:

let maths = [5, 1, 2, 3, 4];
console.log(maths.shift());

Output:

[1, 2, 3, 4]

In the above example, the 5 is removed because it is at the first position in an array.

sort

It returns the element of the given array in sorted order by default it is in ascending order.

Syntax:

sort()

Example:

let sort = [5, 3, 1, 4, 2];
console.log(sort);

Output:

[1, 2, 3, 4, 5]

In the above example, the array is sorted properly.

unshift

It adds elements at the beginning of an array.

Syntax:

unshift()

Example:

let fruit = ['Grapes', 'Banana', 'Apple', 'Oranges'];
fruit.unshift('Mango', 'Pineapple');
console.log(fruit);

Output:

'Mango',
'Pineapple',
'Grapes',
'Banana',
'Apple',
'Oranges'

In the above example, the Mango and Pineapple fruit names are added at the beginning of an array of elements.

converting to array

Converting string to array split() method is used.

Syntax:

array.split()

Example:

let name = ['Matrix'];
let array1 = name.split('');
console.log(array1);

Output:

['M', 'a', 't', 'r', 'i', 'x']

In the above example, the Matrix is a string and it is converted into an array by using the split() method.