programminghead Logo

JavaScript Tutorial 7 : JavaScript Arrays

JavaScript Arrays :

Array is a Special variable in JavaScript who can store multiple Values inside a single VAriable and call any Variable's value by its index Number.

Storing Values in Array :

for storing value in a Array we have to declare a special Variable (array) like (var array_name = [];) and inside the box brackets [] we have to write values for our Special Variable. We can assign numeric and String values to our Special Variable (array) and separate multiple values using single , comma.

JavaScript Array Syntax:


    var myArray = ["value1","value2","value3"];

Calling Array Value :

we can assign multiple value to a single Variable in array and for calling array Values we have to write our Array Variable name and this will export the all Array Values bu if we want to export only one array Value then we have to write array name first and then index number of the assigned value.

Array Index Number :

Array index number starts from 0 and ends according to total number of assigned values in array.

example : var my_array = ["cat","dog","cow","rat"];
var my_array = [0,1,2,3];

here at the index 0 we have cat,
index 1 we have dog,
at index 2 we have cow,
and at index 3 we have rat.

Calling JS Array Example:

Calling JS Array Example :


    var myArray = ["cat","dog","cow","rat"];

    document.write(myArray);    

Results :

Assigning Values to Array using Index Numbers :

There is a another easy and more understandable way to assign value to Array by using Index number, As we know array's index starts from 0 so the first value will have the index number of 0 and by using this method we can easily identify values bu Index numbers.
to do that we have to define a empty array like var myArray = [];
and after terminating ; the declaration line we can assign values by variable name with index numbers like myArray [0]="";.

Values using Index Number

var myArray = []; myArray[0]="cat"; myArray[1]="dog"; myArray[2]="cow"; myArray[3]="rat"; document.write(myArray);

Adding New value to Array :

to assign new value to JS Array we have to write our array name with .push method and inside the round brackets/braces we have to write new value for our Array.

Adding New value to Array Example


    var myArray=["cat","dog","rat"];

    myArray.push("cow");

    document.write(myArray);   

Results

Associative Arrays :

JavaScript not only supports array index number also support array named index. So we can store values inside array Variable by Named Index and also call that value by its Index Name.


    var myArray = [];
    myArray["firstValue"]="cat";
    myArray["secondValue"]="dog";
    myArray["thirdValue"]="cow";

    document.write(myArray["thirdValue"]);

Results

JavaScript Arrays Example

JS Arrays Example


<html>
 <head>
  <title> JavaScript Arrays </title>
 </head>
 <body>

   <script type="text/javascript">

    var cars = [
     "Lamborghini",
     "Ford",
     "Ferrari",
     "Bugatti ",
     "Koenigsegg"];

     cars.push("Aston Martin");

     var totalCars=cars.length;

     document.write("my all cars ="+cars);
     
      document.write("<br/>Total cars Names ="+totalCars);

     document.write("<br/>my car at index 2 is ="+cars[2]);

     document.write("<br/>new added car ="+cars[totalCars-1]);

   </script>
 </body>
</html>	

Results

Comments

how to download minecraft free

http://tinyurl.com/yypeczjg
06:40 am

Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, as well as the content!

minecraft for free

http://tinyurl.com/y6je4rpa
10:22 am

It's remarkable to visit this site and reading the views of all mates about this piece of writing, while I am also zealous of getting know-how.

how to download minecraft free

http://tinyurl.com/yy78f5ne
08:00 pm

Hey! This is my first visit to your blog! We are a collection of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a extraordinary job!

minecraft free download pc

http://tinyurl.com/yxkccvvv
06:13 pm

These are in fact impressive ideas in about blogging. You have touched some pleasant things here. Any way keep up wrinting.

g

http://j.mp/2HazGyQ
10:33 am

Hi all, here every person is sharing these knowledge, thus it's pleasant to read this weblog, and I used to pay a visit this weblog daily.

g

http://tinyurl.com/yye92qch
09:39 am

Having read this I believed it was very enlightening. I appreciate you finding the time and energy to put this content together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worthwhile!

g

http://bit.ly/2HazGyQ
08:01 am

Excellent post. I used to be checking constantly this blog and I am inspired! Very useful info specifically the remaining phase :) I handle such info a lot. I used to be seeking this particular info for a long time. Thank you and good luck.

g

http://tinyurl.com/yypohytg
07:34 pm

I am really impressed with your writing abilities and also with the format for your blog. Is that this a paid theme or did you customize it yourself? Either way stay up the excellent high quality writing, it is uncommon to peer a great weblog like this one today..

minecraft download free

http://tinyurl.com/y5bna9da
08:44 pm

It's appropriate time to make a few plans for the future and it's time to be happy. I've learn this submit and if I could I wish to suggest you few attention-grabbing issues or suggestions. Perhaps you can write subsequent articles relating to this article. I desire to read even more things approximately it!

minecraft download free pc

http://tinyurl.com/y4o4ny7z
02:47 am

Whats up very nice blog!! Guy .. Beautiful .. Amazing .. I will bookmark your web site and take the feeds additionally? I'm satisfied to find so many useful info here within the submit, we want work out extra techniques on this regard, thank you for sharing. . . . . .

minecraft for free

http://bit.ly/2vHEVkk
09:14 am

Hey! This is my first visit to your blog! We are a collection of volunteers and starting a new initiative in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!

gamefly

http://tinyurl.com/y4op5zar
04:56 pm

Hola! I've been reading your blog for a while now and finally got the bravery to go ahead and give you a shout out from Austin Tx! Just wanted to say keep up the fantastic work!

gamefly

http://tinyurl.com/yyrnbqj6
10:26 am

Hi there! I simply want to give you a big thumbs up for the excellent information you have here on this post. I am coming back to your site for more soon.

gamefly free trial

http://tinyurl.com/y2cq62cl
08:29 pm

May I just say what a comfort to uncover someone that truly knows what they're talking about on the net. You actually understand how to bring an issue to light and make it important. More people should check this out and understand this side of your story. I was surprised you are not more popular given that you most certainly possess the gift.

gamefly

http://j.mp/2Lf197O
08:56 am

Why users still use to read news papers when in this technological globe the whole thing is presented on net?

gamefly free trial

http://bit.ly/2LeOly6
02:01 am

Hi, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam responses? If so how do you protect against it, any plugin or anything you can recommend? I get so much lately it's driving me crazy so any support is very much appreciated.

Write to Us