hey what’s going on guys Thun Maya for Telesco learnings and in this video tutorial we are going to be taking an introduction to arrays in JavaScript so if you’ve seen the video of data types in JavaScript in this playlist arrays come under the category of composite or
complex data types and basically arrays in JavaScript are a different type of object and I know we’ve not talked about objects so you’ll understand that as in word but before starting off with the arrays let’s take a scenario here so as you can see in the screen I have three
different variables car one car to car three and basically they’re storing different brands of cars so for now you can see that we only have three different types of cars or three different brands of cars but in real-world scenarios we know that there are many brands and this variable list can go on and on right or let’s say for example in general your program requires that you need to have hundred different variables storing some values let’s say there is a range of numbers that you want to store and you have to create hundred variables so at that time creating individual variables let’s say a b c d and so on and so forth till you exhaust all the alphabets doesn’t make sense right and let’s say what if your program demands that you want to either it or you want to loop through all these variables and if you perform certain operations also so again that becomes more tedious so just imagine that you’re creating hundred different variables andthen you also have to remember the namesand then you have to perform operationson do it and in real-world scenarios yes
you will be creating a lot of variables because your programs are not gonna be like ten lines of code right it’s one of the hundreds of lines of code so this is where the air comes into the picture and what arrays help us do is you can store multiple lists of values under one single
name now if you’re coming from any other programming background the concept of arrays is pretty much the same however
in JavaScript arrays work a little bit different because arrays are dynamic in nature and you can have a different data type of values in that is and we’ll come to that in a minute you will understand it very well but let’s start off with the representation of arrays and let’s create an array in JavaScript so here you can see we have three basic variables now to replace these three variables or what I can do is I can say
where cars I can say equal to opening and closing square brackets which means that it is an array so this is what indicates that it is an erect and inside this I can see double quotes BMW comma double-quotes Morrow comma double-quotes Rd so
as you can see on the right-hand side over here the output is already printed because we are using the life server plug-in and this is one single name so the array name is the card so basically it is a composite data type variable who has one single name but it is storing a list of values right so instead of creating three different variables we just created one and we store the entire list so this is one way of creating
arrays in JavaScript now arrays in JavaScript are indexed and basically what it means that every value which is stored is a top position so it
starts from 0 so BMW is at 0 positions also is at position number 1 and already is at position number 2 so this is how the indexing happens it starts with 0 so if you want to individually access any value let’s say you just want to access the first or the zeroth element where is the first element basically in the cars array what you do is in the output you just put in square brackets and you put the value that you want to access so if I put 0 wire in the output you can see I got BMW if I put the 1 you can see I will get value if I put 2 you can see me
will get already now this is a major advantage because now you can loop through this array using this index so let’s say you want to print these values one line at a time so I can use a for loop over here and we’ve already seen order for loops and different types of loops also so I’m going to say for I’m going to say where I equal to 0 I less than 3 because our index starts from 0 and it ends at 2 so it has to be I less than 3 and I’m going to say I plus and now what I am going to do is just cut this and put it inside the curly braces you can actually exclude the curly braces if you have only one
statement in the for loop so if I arrange this even this is gonna work but typically I don’t do that but you can see now what I’m doing over your is if I just replace this too with I you can see I can get all the values starting from BMW Volvo Oddie because I over your is I iterating in the for-loop so I value keeps on changing and every time we change the value we are getting a new value from the array so we are printing BMW forged involved with Einaudi so this is where you can access the values easily in loops also so let’s say you have 100 values in the car’s variable that is cars array you can easily use a for-loop to access all the hundred values and you can use this hydration concept of loops
so this is not possible if you are storing these values in individual variable straight so this is one more advantage now there is another way in
which you can create arrays you can see where ARR 1 is equal to u so this is a new keyword called as new obviously we’ll talk about you in detail in further videos and then you can see array and opening and closing round brackets you can put in the values if you want so I’m going to say mango and then I’m going to say Apple then we say banana and semicolon and again the same thing if I want to print them I can just change the name of the array which is a or r1 and you can see the values change over here so this is another way typically this is not used it’s not recommended this is the easier way to create arrays but let’s say you want to declare it at first line let’s say
you just see an r1 over here and now you want to make it an array so at that time you have to use the new keyword you have to say r1 is equal to a new array and then inside this, we have to actually pass in the values ok so these are the two different ways we’ll talk more in detail about arrays also but few more things that I want to talk about over here is arrays, as I mentioned in JavaScript, are little different compared to Java programming or C++ because arrays are dynamic in nature so when I say they’re dynamic in nature basically what I mean
is you can add on extra values in the array at runtime
you know so it’s not a fixed size daddy it can always increase in length or decrease in length and you can also add different data types so what I can do is I can see cars dot push and I can add something else so I can say model series and when I loop through the for loop I
can increase the size I can save for and there you can see I got one more value so now you must be wondering what is
this push so as I mentioned earlier arrays are different type of objects in JavaScript and when we talk about objects have certain properties and methods that come along with them okay so since arrays are predefined in JavaScript so you don’t create arrays yourself right so everything that an array does is already there in JavaScript so when you create an array there are some methods that come along
with the arrays and there are some properties also so this not push that is this method is an extra property that comes along with this array similarly if you just type in cars and see a dot you can see the all the number of methods that are associated with that is so all the square or cubes are basically the methods and then there are some properties also you can see this blue length thing so this is a property so we have properties and methods associated
with arrays or any objects typically so this length gives us the current length of the cars so in the for loop what I can do is instead of giving a static value I can see cars not then and then by default the length is going to be equal to the size of the array now the size of the array is the number of elements it has so initially you can see that we had three elements even though the index starts from zero the size is one of you one two and three elements and then we added one more right so now the size is increased to four so that’s
why whatever the size is by default the size will come from over here so if I just comment this out now the size is decreased and the length value is going to be three over here so it can dynamically increase or decrease over the course of time one more thing that I mentioned is you can also add different data type values right so instead of adding a string so right now we have four different strings right what Louise I can add a number also so let’s say I just add and there you go you can see how the array is holding three strings and then we have a number two so this is different because in C++ or Java you know that arrays do not store different data types and then we have different data structures to do that but arrays don’t do that right but in JavaScript it is dynamic also that is the size can decrease and increase over the course of time and it can also store different data types of values in one single array, okay so this was it for this video I know we can talk a lot
about arrays we can talk about different methods you can talk about sorting and all but I don’t want to put it all in this video as we move on we will see some more examples wherein we can use arrays and we’ll perform some operations also so yeah that’s it for this video guys if you like this video please give it a thumbs up you let me know in the comments of this video was share it to heir friends as well and talk to you guys in the next video peace.