Arrays In JavaScript

In this blog we are going to cover Arrays and some of their method.

ยท

3 min read

Arrays In JavaScript

What Is an Array?๐Ÿค”

โžก In JavaScript, array is a collection of data that allows you to store multiple elements at once. Arrays can contain any elements of type String, Number, Objects, boolean, and also other arrays.

const arr = [1,'learnCodeOnline',[1,2,3,true]];

๐Ÿ“Œ Element of an array are acess using index number and everytime the index number start with 0. In above example element[0]=1 element[1]='learnCodeOnline' element[3] = [1,2,3,true]


Some Methods Of an Array๐Ÿ‘€ :-

Let us initiate an array to work with to understand these arrays more clearly.

const arr =["India","USA","Sweden","Norway"];

1.) length( ) -

This method return the number of total elements stored in the array.

console.log(arr.length());
 //4

2.) .push( ) -

This method adds provided elements at the end of an Array. you can pass multiple parameters in push() method.

arr.push("China");
console.log(arr);                      โฌ‡
 //["India","USA","Sweden","Norway","China"];

3) .pop( ) -

This method will remove the last element of an Array.

arr.pop();
console.log(arr);
 //["India","USA","Sweden","Norway"];

4.) shift( )-

This method changes the original array by removing the first item from the array.

arr.shift();
console.log(arr);
//["USA","Sweden","Norway"];

5.) unshift( ) -

This method changes the original array by adding items to beginning of the array.

arr.unshift("India");
console.log(arr);
 //["India","USA","Sweden","Norway"];

6.) splice( ) -

synatx:- arr.splice( start, number of items, replacement items ..... )

This method is used to change contents of an array by removing or replacing elements in an array.

arr.splice(1,2,"spain","Germany");
console.log(arr);
 //[ 'India', 'spain', 'Germany', 'Norway' ]

7.) slice( ) -

syntax : array.slice(start, end)

It returns a new array copying to it all items from index start to end (not including end).

const newarr = arr.slice(1,3);
console.log(newarr);
 // [ 'USA', 'Sweden' ]

8.) concat( ) -

syntax : array.concat(arg1, arg2)

This method accepts any number of arguments -> either arrays or values. The result is a new single array containing items from array, then arg1, arg2.

  const arr =["India","USA","Sweden","Norway"];

 const newarr = ["Haryana","Delhi","Rajastan"];

 console.log(arr.concat(newarr));
  //[
      'India',    'USA',
      'Sweden',   'Norway',
      'Haryana',  'Delhi',
      'Rajastan'
    ]

9.) indexOf( ) -

This method takes element as an argument which we are searching in an array. If it's able to find the element then it will return the index else it will return -1.

 let search = arr.indexOf("Sweden");
 // 2

10.) lastIndexOf( ) -

This method will return the index of the last occurred element.

const arr =["HR","UP","MP","RJ","KA","KL","HR","UP",];

 console.log(arr.lastIndexOf("UP"));

  //7

11.) reverse( ) -

This method is used to reverse the order of an array.

 const arr =["India","USA","Sweden","Norway"];

arr.reverse();

console.log(arr); // [ 'Norway', 'Sweden', 'USA', 'India' ]

12.) sort( ) -

This method is used to sort an array in assecending order.

const arr =["India","USA","Sweden","Norway"];

arr.sort();

console.log(arr);
 //[ 'India', 'Norway', 'Sweden', 'USA' ]
ย