In this article, we will discuss how we can create a dictionary in JavaScript and add key-value pairs in it. Actually there is no ‘dictionary’ type in JavaScript but we can create key-value pairs by using JavaScript Objects. Create a new JavaScript Object which will act as dictionary.
Syntax: Key can be a string , integer. If you just write key1 or any number, it will treat as a string.
var dict = { key1 : value1 ,
key2 : value2 ,
....
};
- Create empty dictionary
var dict = {}; - Adding Key-Value Pairs in Dictionary
dict[new_key] = new_value;
or
If new_key is already present in the dictionary then the value of this will be updated to new_value.dict.new_key = new_value;
- Accessing Key-Value Pairs
var value = dict[key];
or
var value = dict.key;
- Iterating the whole dictionary
for(var key in dict) { console.log(key + " : " + dict[key]); }
Example:
HTML
<!DOCTYPE html> <html> <head> <title>Dictionary in Javascript</title> </head> <body style="text-align: center;"> <h1 style="color: green;"> GeeksforGeeks </h1> <p> var dict = { <br /> "geek" : 1 , <br /> "for" : "2", <br /> "geeks" : 3.5 <br /> }; <br /> </p> <button onClick="fun()"> Add new Key-Value Pairs </button> <p id="demo"></p> <!-- Script to create dictionary and add key-value pairs --> <script> function fun() { var dict = { geek: 1, for: "2", geeks: 3.5, }; dict.new_geeks = "new_value"; dict["another_new_geeks"] = "another_value"; var to_show = "var dict = { <br>"; for (var key in dict) { to_show += '"' + key + '" : ' + dict[key] + "<br>"; } to_show += " }; <br>"; document.getElementById("demo") .innerHTML = to_show; } </script> </body> </html> |
Output:

Recommended Posts:
- How to dynamically create new elements in JavaScript ?
- How to Create a Form Dynamically with the JavaScript?
- Create a Form Dynamically using Dform and jQuery
- How to create an image element dynamically using JavaScript ?
- How to create button dynamically with click event in Angular ?
- Create pandas dataframe from lists using dictionary
- Python program to create a dictionary from a string
- How to create an unordered_map of pairs in C++?
- How to create an array with key value pairs in PHP?
- How to dynamically load JS inside JS ?
- How to change favicon dynamically?
- Dynamically generating a QR code using PHP
- How to generate an XML file dynamically using PHP?
- Dynamically Execute JavaScript in ElectronJS
- How to dynamically get the content height of a div using AngularJS ?
- How to dynamically get the content width of a div using AngularJS ?
- How to add elements to an existing array dynamically in JavaScript ?
- How to dynamically change the title of web page using JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- Change Image Dynamically when User Scrolls Down | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


