Home Java Javascript-array JavaScript Array Associative



JavaScript Array Associative
Posted on: November 10, 2008 at 12:00 AM
In this section, you will study how to use Associative Array in Javascript.

JavaScript Array Associative

     

In this section, you will study how to use Associative Array in Javascript.

An associative array is like a regular array, but we insert the string as the index value instead of numbers used in regular arrays. It is the another way of storing information. In the given example, we have created an array object 'information' and added the latest car models with their information. Now, you can get the information of the latest car by entering the name of car which are defined in the array when the script prompts the user to enter the car.

 

 

 

 

Here is the code:

<html>
<head>
<h2>Associative Array Example</h2>
<script language="JavaScript">
function carInfo()
{
var information= new Array()
information["Nano"]="Nano has been made by Tata Motors.It is a small car.";
information["Cruse"]="Cruse has been made by Chevrolet.It is a luxury car.";
information["Hummer"]="Hummer has been made by General Motors.It is a Luxury car.";

var info=prompt("Whose information do you want?","");

if ((info=="Nano") || (info=="Cruse") || (info=="Hummer"))
alert(information[info]);
else
alert("No Information");
}
</script>
<form>
<input TYPE="button" onClick="carInfo()" value="Upcoming Cars">
</form>
</head>
</html>

Output will be displayed as:

On clicking the button, it will prompt the user to input the car name:

On clicking the ok button, it will display the car information:

Download Source Code

Related Tags for JavaScript Array Associative:
javajavascriptcarrayscriptioipthisssostudytocieilsectionociuseassociativeinasjhowssoatisllivrayarsocvascrssriripthavstationo


More Tutorials from this section

Ask Questions?    Discuss: JavaScript Array Associative  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.