Introduction to JavaScript Array Methods

Javascript Array Methods

      To understand the JS array methods first we should know what is an array in Javascript. 
      Array is an object which can store multiple elements at time. In JS array we can store the variety of elements at time.   
       Like every array, here we can also access the all elements through indexing. 

Basic array methods

  1. shift()
  2. unshift()
  3. push()
  4. pop()

shift()

shift() method is used for removing the elements from array.It removes first element from array.

var city = ["Nagar","Mumbai"];

city.shift()

//output will be :- Mumbai

unshift()

unshift() method does reverse of shift() method. It adds the element at the beginning of an array.

var city = ["Delhi","Mumbai"];

city.unshift("Pune");

//output  Pune,Delhi,Mumbai

push()

push() method used is for adding the element. push() add the element at the end of array.

var names = ["tony","fellani","paul"];

names.push("messi");

//output   tony,fellani,paul,messi

pop()

pop method used for removing the element from array. It removes the last element of an array. It does not take parameter.

var fruits = ["Apple", "Cherry","Banana"];

fruits.pop();

//output     Apple,Cherry