what's up guys welcome to this blog once again. Today we will learn about arrays in JavaScript
, You know how to create variables and assign there value you also know that there ara data types like
integer , float , string etc. I mean with one variable you can store data of similar data type BUT what if you want to
store data of different data type in one place ? In that situation Array comes in action.
Array is similar to a variable in which you can store / hold any type of data to clear your doubts let me give you an example
As you already know that a variable an store one piece of data at a time and when the value of that variable is changed
the previous value is lost like take the example below:-
var myVar = 10; // The value of variable myVar is 10 now
alert(myVar) //This will open a alert box with value 10
/*---------------------------------------------
*
But what if i again assign the value of
variable myVar to 22 and use the alert
method to display the value of variable myVar?
Yes you are thinking right the new value on
alert box will be 22 and the previous value 10
will be lost.
*
-----------------------------------------------*/
var myVar = 22; // The value is changed now
alert(myVar) // Previous value is lost and we have now 22
The main difference between a variable and an array is , A variable can hold only one piece of data at a time
but an array can store more than one piece of data at a same time i mean you can store the both value 10 and 22
into an array .
Creating an array in JavaScript
Let me remind you once again that JavaScript is a case sensitive language and if you use array() rather than
Array() , the code will not work
so , to create an array you have to declare the variable first and then tell JavaScript that you want this variable as
and Array.
var myArray;
myArray = new Array();
/*-----------------------------------
This was method of declaring an Array
now if you want to store data into
your new Array you have to follow this
--------------------------------------*/
var myArray = new Array(1,2,3,4,5,6);
/*--------------------------------------
Just put your data between parentheses
separated by comma(,) and you are done
----------------------------------------*/
/*---------------------------------------
you can put strings also , not only numbers
-----------------------------------------*/
var myArray = new Array("Ved", "love" , "Code");
/*-----------------------------------------
Even you can mix the type of data in an
Array
-------------------------------------------*/
var myArray = new Array("Hello","ved",22, null);
Indexing of an JavaScript Array
Always remember that we humans start counting from one(1) not zero(0) but computers are
not like that , computers start counting from zero(0) not (1).
that's means first piece of data will store at position 0,
second piece of data will store at position 1,
third piece of data will store at position 2 and so on
for example lets take the
example of our previous Array
var myArray new Array("Hello","ved",22,"code")
/*-----------------------------------------
If we start counting ( like humans ) then
we will index the items of this Array like
*-*-*-*-*-*-*-*-
| Hello - 1st, |
* ved - 2nd, *
| 22- 3rd, |
* code- 4th *
*-*-*-*-*-*-*-*-
but on other side computer will index this
array as follow
*-*-*-*-*-*-*-*-*-*-
| Hello - 0(zero), |
* ved - 1st, *
| 22 - 2nd, |
* code - 3rd, *
*-*-*-*-*-*-*-*-*-*-
-------------------------------------------*/
Adding data into an JavaScript Array
It is not necessary to pre-store data in a JavaScript Array, That's mean you can create an Empty Array
and later then add data to that Array with indexing like thale the example above
var myArray = new Array(); //we create an Emplt array here
//now we will add data to this Array with Indexing
myArray[0] = "I";
myArray[1] = "love";
myArray[2] = "coding";
myArray[3] = 24;
myArray[4] = "X";
myArray[5] = 7;
/*
---------------------------------------------------------
after that to check that the data is stored in myArray or
not you can do that with alert(myArray) and it will display
the values stored in myArray
-----------------------------------------------------------
*/
Using .push method to add data into an Array
You can also insert/add data into an JavaScript Array with .push method
if our Array is an Empty Array then the Data will store at position Zero otherwise
if there are 10 items in your Array then your new item will be stored at position
ten(remember that computer start counting from zero)
have a look at the Example Below we will use the .push method with our
previous Array
var myArray = new Array(); //we create an Emplt array here
//now we will add data to this Array with Indexing
myArray[0] = "I";
myArray[1] = "love";
myArray[2] = "coding";
myArray[3] = 24;
myArray[4] = "X";
myArray[5] = 7;
myArray.push("hello",34); //The data will be added
alert(myArray); // Display the result
All right folks i think this is enough for this post , in next post we will learn more on Arrays
in JavaScript like and some other feautures of it like push , pop , slice , shift , sort etc.
Thanks for reading guys please comment below if you facing any problem :)