hello guys welcome to the next blog on JavaScript tutorial for beginners in this blog we will see how to use arrays in JavaScript now first of all what is an array an array is a variable which is capable of storing more than one value okay so using array you can store multiple value in our variable now let's see how we can declare an array in JavaScript so you use var to declare an array as any other variable and then the name of a variable for example we want the collection of name or multiple names in our variable and then you use the square bracket to declare them and for example I'm going to add some names here something like this so this are a name has four names inside it so it's storing four different names right now to access this array for example you want to access the first element of this array how you can do it so what I'm going to do here is I'm going to just print this value using document dot get element by ID and I have this paragraph tag here whose ID is demo which I'm calling here dot inner HTML is equal to and for example I want to print the whole array so I can just call this like this okay and save your code and refresh it and you will see here all the names printed on the webpage right so arrays has some indexes and indexes is nothing but the position at which the value is stored and this value starts from zero in the case of array so this position of the first value is zero index because the index start from zero so the index of Tom is zero John is one and Bill is two and Mark is three right so for example I want to access this name John so I can just right here name and then in the square bracket one and one is the index here okay and save your code and refresh it it's printing John now for example I want to access this name Tom here the index of it is zero okay and just save it and refresh it and then it will give you Tom right so indexes in array start from zero now if you want to change some item at some index what you can do here is you can just call the name of your array and then the index on which you want to change so for example I want to change this name Tom and I want to make this name starting with capital T not with small T so I will just use name zero and then is equal to the new name right so the new name is Tom with the capital T right and let's just save your code and now refresh it and now you can see the name becomes Tom with capital T right now using this method you can also add some items to the array so right now I have four items in my array and the index of these items are zero one two three so I can add at the fourth place an item so I can just write four and in here I can add a new item so something like this and then I can print my array something like this save your code and refresh it and now my array has five items in it now there is one more way of pushing element in the array so for example you want to add one more item in the names so you can use names dot push and then you can just add another new name so for example some new name something like this okay and then save your code and refresh your webpage and now you can see one more item is to your array so you can use push or using this index method to add to an array now there are some method associated with arrays which you can use to use the arrays now for example I want to know how many number of elements are contained inside the array so I can use a method called the length so just use the name of the array which is names in my case then use dot and then call length method on it and then it will give you the number of elements inside the array right so save your code and refresh it and it says six which is true because these are four names and these two names we have pushed outside the array right now there is one more method which you can call on the array which is called sort and this will sort all the element in the array for example you have names here so it will sort the names according to the alphabetical order right so save your code and refresh it and now you can see these names are sorted in alphabetical order so be first and then J then M then T right now it's not necessary that array must have the elements of same data-type so for example I want strings and integer in my array it's totally valid so for example I'm going to delete this name and instead of this name I'm going to add this integer 500 right and save my code and refresh my webpage and it's going to add this integer to the array and it's no problem right now there is a different way of declaring an array also and which is something like this so var name one for example is equal to new array and then in the brackets you can provide the elements okay so for example I can just copy this and paste it here and it's totally valid okay and I'm going to declare it above here and I'm going to call this name one here and save it and refresh my web web page and it's going to give me the same result as we have seen earlier okay now you can use some more examples with arrays for example I will delete this array declaration and this also and I will use this name and I will call a method called pop okay and what this method does is it removes the last element from the array okay so I will just call this array here and save my code and refresh it and you can see I only have three elements in the array fourth is removed okay now if you want to remove the first element from the array Uppal a method called a shift here okay and it's going to remove the first element from the array so let's save it and refresh the webpage and now you can see the first element tom is removed and the second third and fourth elements are here okay now if you want to add some element to the first place for example at index zero you can use unshipped methods so just write names dot on shift and then you can add the value here for example Ben and then save your code and refresh the webpage and now you can see at the 0th index this name is added so in this way you can use arrays in JavaScript I hope you have enjoyed this blog