Learning Javascript Part III
Arrays in Javascript
Earlier we saw how we can store values in variables in javascript so that we can use them later on. But what if we have a lot values say 5000, then creating 5000 variables for these 5000 values will be a ridicules idea. So what's the solution for this problem? Well we can use an Array store these values. So what exactly is an array. Well for all those definition loving guys here's one,
"An array is a data structure used for storing a collection of values."
And for those who are still confused , An array is a collection of homogeneous data. Its declaration looks like this;
The above array is called empty array as there is nothing in it. So how we use arrays.
Lets take a classical example, lets say you are class teacher and you want to store names of your students Roll No wise but there are 40 students now one tedious way to do this is to create 40 variables like this;
Or you can just create an array named students like this;
In above example we haven't given values to our array, to do so, we can do initialization of array like this;
Its not necessary to give one type of data in an array. You can mix data like this;
In array Values are stored like this,
So the first value you enter is stored at position 0 and second one is at position 1 and so on. To access these values later we can just write;
arrayName[0]; //returns first element of the array
arrayName[1]; //returns second element of the array
Similarly, you can add elements to array with assignment operator like this;
arrayName[0] = 45;
arrayName[1] = "Beautiful";
Properties and method
Arrays have some properties and methods in javascript. Properties tell you something about the array, while methods may be used to change something in the array or return a new array. Lets look at these,
- Or turn an array into string
This is it for this post guys so well hope you guys enjoyed this post.Please like, share this post and subscribe to DeeCoder.
Tell me your questions and what do you think about this post in comments.
Comments
Post a Comment