Data Type  In Javascript-array And Its Methods

Data Type In Javascript-array And Its Methods

What is array?

Array is anything which can store more than one value.

Array is secondary data type.

Array stores the value by index. It will be clear in below example.

//           0      1        2          3         4          5          6         7

let arr=[    11 ,    2 ,    345 ,    54  ,    22  ,    123  ,    44  ,    56  ]

1st element is stored at 0th index

2nd element is stored at 1st index

3rd element is stored at 2nd index

4th element is stored at 3rd index

and so on....

If we want third element of above array we can write :

let third=arr[2]
console.log(third)    //345

Similarly if we want 1st element of this array we can write :

let first=arr[0]
console.log(first)     //11

We can update any element of array as follows:

let numbers=[0,11,2,34,4,5,6]
numbers[6]=999999
console.log(numbers)   //[0,11,2,34,4,5,999999]

Everything in JavaScript is object so array is object and every object has its properties and methods.

Property of an array is its length. Length of array means how many number of elements are present in array.

let array1=[12,34,55,67,32,56,42,90]
let lengthOfArray=array1.length
console.log(lengthOfArray)       //8

Methods of an array :

Every method has its action and return type.

Taking real life example=> When method is study, actions are reading and writing and return type is result i.e. pass or fail.

So let's start the methods of array.

In this blog we will discuss only four important methods of array to remove or add first or last element of array.

1)push( ) =>

action : adds element at the end of the array

return type : array (returns new array)

example:

let names=["John","Rahul","Danny"]
let a=names.push("Jeniffer")
console.log(a)     //["John","Rahul","Danny","Jeniffer"]
console.log(names)   //["John","Rahul","Danny","Jeniffer"]

let num=[12,34,56,78,99]
let b=num.push(1000)
console.log(b)      //[12,34,56,78,99,1000]
console.log(num) //[12,34,56,78,99,1000]

2)pop( ) =>

action: remove last element of array

return type: single element (element which is removed)

example:

let sq=[1,4,9,16,25]
let c=sq.pop() //do not have to pass any arguments
console.log(c)          //25
console.log(sq)       //[1,4,9,16]

let string=["Whatsapp","Facebook"]
let oo=string.pop()
console.log(oo)    //Facebook
console.log(string)  //["Whatsapp"]

3)shift( ) =>

action : remove first element of array

return type : single value (element which is removed)

example :

let no=[2,4,6,8,10]
let d=no.shift()
console.log(d)     //2
console.log(no)   //[4,6,8,10]

let countries=["Korea","America","India","Japan"]
let rrr=countries.shift()
console.log(rrr)      //Korea
console.log(countries)  //["America","India","Japan"]

4)unshift( ) =>

action : adds element at the starting of the array

return type : array (returns new array)

example :

let num1=[12,34,56,78,99,7]
let e=num1.unshift(9999)
console.log(e)     //[9999,12,34,56,78,99,7]
console.log(num1)    //[9999,12,34,56,78,99,7]

let str=["Pune","Newyork","Mumbai"]
let jj=str.unshift("Delhi")
console.log(jj)      //["Delhi","Pune","Newyork","Mumbai"]
console.log(str)    //["Delhi","Pune","Newyork","Mumbai"]

In next blog we will cover another four methods of array...