Arrays And Its Methods In Javascript.

ยท

3 min read

Hey folks, in this blog i would like to talk about arrays and its methods. we will see what an array is, how we can add elements inside an array and what are the methods we can use to access those elements in the array. Ok so first lets see what an array is-

Array

In simple terms we can say that, an array is basically an ordered collection of data. These data can be of many types for example strings, boolean, number and mixed too. we store these data inside square bracket's '[ ]' separated with a comma( , ). These data's can be modified and retrieved any time by the user by using pre defined methods in javascript. so lets see some examples of arrays.

Creating Array - have a look at this for reference on how to create an array.

// to make an empty array
var list=[];
// to make an array of of string
var name=["rahul","yuvraj","saurabh","vikas","manish"];
// to make an array of number's
var primeNum=[2,3,5,7,11];
// to make an array of boolean
var isResult=[true,false];
// to make an array of miked data's
var stuff= ["rahul",29,"true];

Also not to forget that arrays are indexed, which means every element inside an array has a separate index value which starts from 0. For example -

// to check index of "rahul" in name array 
name.indexOf("rahul")
// output will be 0.

Methods

Methods are basically used to access all the data's from an array. In javascript we can use all these pre-defined methods to access the element inside an array. So lets have a look at some of the methods -

1. push( ) - this method is used to add extra element to the very end of an array for example-

let num=[19,20,21 ]
// now to update this using push()
num.push(22)
// the output will look like this
num=[19,20,21,22]

2.pop( ) - this method is used to remove the last element from the very end of an array. For example-

let num=[19,20,21]
// now to update this using pop()
num.pop()
// the output will look like this
num=[19,20]

3.shift( ) - this method is used to remove the element from the start. For example-

let name=["rahul","saurabh","yuvraj"]
// by using shift()
name.shift()
// the output will be
name=["saurabh","yuvraj"]

4.unshift( ) - this method adds the element from the start to an array. For example-

let list=["bread","toast"]
// by using unshift()
list.unshift("egg")
// the output will be
list=["egg","bread","toast"]

5.concat( ) - this method is used to combine elements of two arrays together. For example-

let num=[19,20,21]
let alphabets=["a","b","c"]
// by using concat()
let combine =num.concat(alphabets)
// the output will be
combine=[19,20,21,"a","b","c"]

6.reverse( ) - this method is used to reverse the order of the elements inside the array.

// for example we have
let num=[19,20,21]
// with reverse()
num.reverse()
// output will be
num=[21,20,19]

7.splice( ) - splice can be used to add elements inside an array and also to remove elements from an array. For example-

syntax is= array.splice(index,deleteCount,itemToAdd)

// for example we have
let list=["bread","biscuit","milk","eggs"]
// by using splice()
list.splice(1,1,"chips")
// output will be
list=["bread","chips","milk","eggs"]

Thank you for reading ๐Ÿ˜Š๐Ÿ˜Š..

connect with me here ๐Ÿ‘‡

twitter || Github

ย