<html> <body> <script> var hashtable = {} hashtable["ABA"] = 13 hashtable["CAB"] = 12 hashtable["BAC"] = 15 for (key in hashtable) document.write("The key is : ", key, " and the value is : ", hashtable[key], "</br>") i = hashtable["ABA"] document.write("ABAs age is ",i, "</br>") j = hashtable["BAC"] document.write("BACs age is ",j, "</br>") </script> </body> </html>
Luckily, JavaScript already provides Object that represents HashTable.
As we can see we have stored the names ABA, CAB and BAC and their corresponding ages 13, 12 and 15 in the Hashtable.
hashtable["ABA"] = 13 hashtable["CAB"] = 12 hashtable["BAC"] = 15
But how they are stored in the HashTable will be decided by JavaScript. As we are using the Object provided by JavaScript.
So, for ABA, a Hash Code is calculated by JavaScript and the age of ABA is stored in some location decided by JavaScript.
Similarly, the age of CAB and BAC is stored in the Hashtable after JavaScript calculates the Hash Code, and decides where they will be stored.
After all the values are stored, we then check all the values stored in the Hashtable.
for (key in hashtable) document.write("The key is : ", key, " and the value is : ", hashtable[key], "</br>")
Then we try to retrieve the age of ABA.
i = hashtable["ABA"]
So, we pass the name of ABA and internally JavaScript calculates the Hash Code for ABA and gives us the age of ABA from the Hashtable.
And we print the age of ABA on the screen.
document.write("ABAs age is ",i, "</br>")
Similarly we retrieve the age of BAC and print it on the screen.
j = hashtable["BAC"] document.write("BACs age is ",j, "</br>")