Array and  Array Methods:-

Array and Array Methods:-

In the last articles, we talk about javascript and its datatypes of which Array is one of them. Array is non primitive Datatypes ,its assign more than one elements at a time.

so in this article, we are going to talk about Array and its methods....let's start today's articles with the definition of an Array and how to access elements of the array. This article is totally about the Array and its Methods. be connected...

Array:-

It is a collection of data or stores multiple data at a single variable. It is a nonprimitive data type and very popular for storing more than one data in a single variable. we can perform various tasks by using Array.

In an array, we can usually see that square bracket in which we collect data from more than one, whether it is a number, strings, or both in only one variable that we declare. let's take an example and proceed...

//we declare a variable that store data in square bracket

  let arr=[1,2,3]     // only number datatypes
  let arr1=["apple", "banana","guava","mango"]   //only strings datatype
  let arr2=[1,"apple",2,3,"mango"]           //number as well as strings


                                  //output:-
  console.log(arr)              //  arr  =[ 1, 2, 3 ]
   console.log(arr1)            // arr1=[ 'apple', 'banana', 'guava', 'mango' ]
  console.log(arr2)             // arr2=[ 1, 'apple', 2, 3, 'mango' ]

Access Elements of the array:-

In the array, elements are assigned as an index or it simply means in which index or position elements are in the array. It starts from 0 and goes to the index. length. index. length means the last element in the array.

we can access elements by using their index value when we want to print the value of all the elements present in the array then we call it index which gives the elements as well as their index. It is also known as the length method of the array which returns us the index.

index=i last elements=index.length

   array=[1, 2, 3, 4, 5, 6]  // we indexing the elements
  // index= 0,1, 2, 3, 4, 5          the last element is an index.length -1 (6-1=5)
  console.log(array)           //output:-[ 1, 2, 3, 4, 5, 6 ]
 let array=['apple', 'banana', 'guava', 'mango']
    for(let i=0;i<array.length;i++){      // start from 0 till index.length
    console.log(i)                     // output:-  0     1      2      3
     console.log(array[i])              //output:-apple banana  guava mango
  }

Array Methods:-

Array provides a lot of methods to things pretty much easier. so let's talk about some of them...

push() and pop():-

push():- Insert new elements at the end inside the array.

 let array=[1,2,3,4]  // declare a variable
 array.push("apple")   // insert a new element

 console.log(array)    //output:-[ 1, 2, 3, 4, 'apple' ]

pop():-Remove elements from the last. It has modified original array as well.

 let array=[1,2,3,4]  // declare a variable
 array.pop()   // remove the last element

 console.log(array)    //output:-[1,2,3]

slice() and splice():-

slice():- It is used to slice the elements or we can take out the given index value .it take two parameters one is the start index and the second is the last index. The last index is exclusive.

let array=["apple","banana","guava","mango", "papaya"] //  declare variable
console.log(array. slice(1,3))   // it takes parameter 1 is 1st index and
                            //3 is the last index output:-[ 'banana', 'guava' ]

splice():-this method takes two parameters and replaces the current value of that index, two parameters are the index of elements and they replace the elements according to the parameter we give.

 let array=["apple","banana","guava","mango"]
                                     array.splice(2,0,"badaapple","chotaapple")  
 console.log(array)  
 //output           ['apple','banana','badaapple','chotaapple','guava,'mango']

                                     array.splice(2,1,"badaapple","chotaapple")   
          console.log(array)
//output-['apple','banana','badaapple','chotaapple','chotaaple','guava,'mango']

sort() and reverse():-

sort():-It is the type of method that change the elements according to ascending means it rearranges the element according to their alphabetical order or ascending number.

let array=["monika","swati","beauty","jyoti"]
  let array1=[10,24,15,20]

  array.sort()
  array1.sort()
  console.log(array)   //output:-[ 'beauty', 'jyoti', 'monika', 'swati' ]
  console.log(array1)  //output:-[ 10, 15, 20, 24 ]

reverse():- It gives the reverse array. Reverse the all elements present in the array and the index.

let array=["monika","swati","beauty","jyoti"]

  array.reverse()
  console.log(array)  //output:-[ 'jyoti', 'beauty', 'swati', 'monika' ]

shift() and unshift():-

shift():-it removes the first element of an array and it is modified original array as well.

let array=["monika","swati","beauty","jyoti"]
 console.log(array.shift())   //output:-monika
 console.log(array)   //output:-[ 'swati', 'beauty', 'jyoti' ]

unshift():-It inserts or adds new elements at the beginning of the array.

let array=["monika","swati","beauty","jyoti"]
array.unshift("satyam")
console.log(array) 
 //output:-[ 'satyam', 'monika', 'swati', 'beauty','jyoti' ]