Share on Google+Share on Google+

JavaScript Array Associative

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:

<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("No Information");
<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:

Download Source Code


Posted on: November 10, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: JavaScript Array Associative  

Post your Comment

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