Ads by ProfitSence
Close

Associative array/hashing in JavaScript

Last Updated on Wednesday 5th Oct 2022

javascript hash table

  • Hash Tables are a data structure that allow you to create a list of paired values.
  • Common Example of a Hash Table in JavaScript is the Object data type.
  • You can pair the object's property value with a property key.
			
					let obj = {
  fname: "Alan",
  lname: "Doug"
}

			
	

javascript hash

  • JS hasOwnProperty() method which allows you to check if a property is not inherited.
			
					const obj = {};
obj.fname = "Alan";

console.log(obj.hasOwnProperty("fname"));

			
	

hash tables javascript

Associative Array: It use Strings instead of Integer numbers as index.

Create an object with

			
					var dictionary = {};

			
	
  • JavaScript allows you to add properties to objects.
			
					Object.yourProperty = value;

			
	

An alternate syntax for the same is:

			
					Object["yourProperty"] = value;

			
	

javascript hashtable

			
					var dictt = { name:"Alan", age:24, job:"Writing"  };

// Iterate over using keys
for (var key in dictionary) {
  console.log("Key: " + key + " , " + "Value: "+ dictionary[key]);
}

console.log(dictt.name)