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:
<h2>Associative Array Example</h2>
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"))
<input TYPE="button" onClick="carInfo()" value="Upcoming Cars">
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:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: November 10, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles