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.

JavaScript Array Associative

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