Sometimes we need to deal with a list of items of the same types. So for example, you have a shopping cart of items, and you need to add all of these items together. Unfortunately, we have something called arrays in JavaScript. We which will get the job done for us. So let's now create an array. For example, we have an array of fruits.
And it's equal to square brackets. This is how we create an array by using square brackets. And inside this square bracket, these square brackets, we add these items. So for example, we have Apple, and then we have orange. And then finally we have banana. So this is called an array.
Now let's print it in our console. So console dot log, and we pass this array which is fruits, fruits, and save and run. And as you can see in our console, we have this array, apple, orange banana. But what if we want to access only one element from this array. And in order to access only one element, we use something called the index, and the index will point to one element inside our array. So for example, if we want to access the apple we would just use the index of zero because any array is starts with zero, so the apple would take the index of zero, the orange orange would take the index of one and banana would take the index of two.
So if I want to access the first element of this array, I would add square brackets and Past zero inside these square brackets. And if I save and run, as you can see i i can see now Apple because I was able to access the first element of this array. And also there is a very good function that we can use to know the number of elements inside this array. And the number of elements as you can see 123 and the function is called length. So we can call this function by using dot and then length and save and run. And as you can see, now it says the three elements because one we have 123, but the index is 012.
So there is a there is a difference between the index and the number of elements. The number of elements are just the number of elements 123, but the index starts with 0012 So this is how you can create an array and later you will see how important they are when you create your applications