Home Java Javascript-array JavaScript Array Associative

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript Array Associative

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 10, 2008

Related Tutorials

Discuss: JavaScript Array Associative  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com