JavaScript Array

JavaScript Array is a single variable that stores different values at once. In JavaScript , it is a type of object so it inherits from the Object prototype. Array can be created as following methods:

const arr = [1, 2, 3];

const arr = new Array(‘a’, ‘b’, ‘c’);

Array is of fixed sized and all items has index which starts from 0 and ends to length of array-1.

Both methods creates a Array so any method can be used to create a Array.

Also learn more about JavaScript Basics

Array is an Object so it inherits from Object prototype so it can have methods which it can use inherited from object and methods which it gets from Array prototype. You can learn more about Array on JavaScript Docs.

This are the methods array gets from Array prototype. And following are the methods which it gets from object prototype:

These are properties from Object prototype in JavaScript Array

Methods and Properties of Array

length()

Finding Length of Array :

Length can be found using .length method:

const arr =[1, 2, 3, 4];

console.log(arr.length);

This returns 4 which is length of arr.

Finding first item in Array:

const arr =[1, 2, 3, 4];

console.log(arr[0]);

This returns 1 as it is the first digit of array.

Last Item in Array:

const arr =[1, 2, 3, 4];

console.log(arr[arr.length-1]);

This returns 4 as it is the last digit of array.

indexOf()

Finding index of item :

This method is used to find index of first item if multiple item matches the item given;

const arr =[1, 2, 3, 4];

console.log(arr.indexOf(1));

This returns 0 as it is the index of array . It returns the index of first matching item.

lastIndexOf()

Finding last index :

This method is used to find index of last item if multiple item matches the item given;

const arr =[1, 2, 3, 2];

console.log(arr.lastIndexOf(2));

This returns 3 as it is index of last matching item.

concat()

Joining Two Arrays:

This method is used to Join two arrays:

const arr =[1, 2,3];

const arr2 =[4, 5,6 ];

const arr3 = arr.concat(arr2);

console.log(arr3)

This returns [1, 2, 3, 4, 5,6] by joining two arrays.

unshift()

Adding Item at beginning of Array:

const arr= [1,2,3];

arr.unshift(4);

console.log(arr)

This returns [4,1,2,3] as this method adds item at beginning of array.

shift()

Removing Item from beginning of Array:

const arr= [1,2,3];

arr.shift();

console.log(arr)

This returns [2,3] as this method removes item from beginning of array.

push()

Adding Item at last of Array:

const arr= [1,2,3];

arr.push(4);

console.log(arr)

This returns [1,2,3,4] as this method adds item at last of array.

pop()

Removing Item at last of Array:

const arr= [1,2,3];

arr.pop();

console.log(arr)

This returns [1,2] as this method removes item at last of array.

reverse()

Reversing the Array:

const arr= [1, 2,3];

arr.reverse()

console.log(arr);

This returns [3,2,1] as this method reverses the array.

includes()

Finding item exist on array.

const arr=[2,3,4];

console.log(arr.includes(4));

This returns true as 4 exist on the Array.

toString()

Changing array into string.

const arr= [1,2,3,4];

console.log(arr.toString());

This returns 1,2,3,4 which is string representation of the array.

join()

Joining items of array.

const arr = [1,2,3,4];

console.log(arr.join(“”));

This returns 1234 as it joins items in the array.

slice()

Removing item from any index

This can be achieved using slice method

const arr =[1,2,3,4];

console.log(arr.slice(2,3));

This returns [3] as 3 lies at index 2 given in slice method and before 3 index.

arr.slice(2) returns [3,4] as this makes ending as end of array.

This method creates a new array and doesn’t modify existing array.

Higher order methods in JavaScript

These are the methods started from es6 JavaScript which provides many methods in the array

 prototype . Some of the methods are:

forEach()

 This method loops over the item. This method has distinct syntax which is

const arr= [1,2,3,4];

arr.forEach((item)=>console.log(item));

This returns:

1
2
3
4

as this method loops over the array . This method can be seen as simpler version of for loop.

map()

Array map also loops over array but returns new array

const arr =[1,2,3,4];

console.log(arr.map((item)=>item+1));

This returns new array where each item is increased by one that is [2,3,4,5].

reduce()

This method also loops over the array but it reduces to singular value.

const arr =[1,2,3,4];

console.log(arr.reduce((item, sum)=>item+sum, 0));

This returns 10 as this method reduces to singular value that is sum of all values.

filter()

This method loops over the array and filters the array according to given requirement.

const arr =[1,2,3,4];

console.log(arr.filter((item)=>item<=2));

This returns [1,2] as it satisfies the conditions in the filter method.

some()

This method loops over the array and checks if the array contains some items.

const arr =[1,2,3];

console.log(arr.some((item)=>item>5));

This returns false as there is no item in array greater than 5.

sort()

This method loops over the array and sorts the array.

const arr =[1,20,33,4];

console.log(arr.sort((a,b)=>a-b));

This returns [1,4,20,33];

The arguments are required for sorting numbers but for string sort it doesn’t require it

const arr= [“s”,”a”,”c”]

console.log(arr.sort());

This returns [“a”,”c”,”s”] ;

Be up to date with the digital world with Enlight Info.

Leave a Reply

Your email address will not be published. Required fields are marked *