All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Key, Value Map

Last Updated : Mar 11, 2024

JavaScript Key, Value Map

In this tutorial we will show you the solution of JavaScript key, value map, map is a collection of elements where each element is stored as a key, value pair.

For showing mapped key,value pair here we will showing demo within that we defined new map object with initialized pair array.

Then result printed on console using console.log() method. Using map object we can create more types string pairs, or array pairs with both whole number and strings.

Step By Step Guide On JavaScript Key, Value Map :-

Here we defined map object with key, value pairs. Map object can hold both objects and primitive values as either key or value.

When we iterate over the map object it returns the key, value pair in the same order as inserted.

It can also hold array within array and key array, value array is respective key name values.

Those are defined with example in our program when you will executes you can get some idea about that.

<!DOCTYPE html>
<html>
    <head>
        <title>Map in Javascript</title>
    </head>
    <body>
        <script>
            var map1= new Map([[1,2],[2,3],[4,5]]);
            console.log("Map1");
            console.log(map1);
     var map2= new Map([["whole numbers",[1,2,3,4]],
            ["negative numbers",[-1,-2,-3,-4]]
            ]);
            console.log("Map2");
            console.log(map2);
            var map3= new Map([[["first name","last name"],
            ["dhanu","pavi"]]
            ]);
            console.log("Map3");
            console.log(map3);
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Within <script> tag we defined our javascript program, here we initialized map objects ‘map1,map2,map3’ with different types of key, value arrays.
  7. In map object ‘map1’ we defined array with ‘[1,2],[2,3],[4,5]’ values, here keys are ‘1,2,4’ with respective values ‘2,3,5’.
  8. In map object ‘map2’ we defined array within array ‘["whole numbers",[1,2,3,4]],["negative numbers",[-1,-2,-3,-4]]’ values, here keys are ‘whole numbers, negative numbers’ with respective another array values ‘[1,2,3,4],[-1,-2,-3,-4]’.
  9. In map object ‘map3’ we defined array pair is key array ‘["first name","last name"]’, value array ‘["dhanu","pavi"]’.
  10. Printed all results using ‘console.log()’ method. Console.log() is a function in javascript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Its output display on the javascript console.
  11. In browser for open javascript console use the shortcut ‘ctrl+shift+j’ then console panel will open at right hand side there we can see our result.
  12. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know how to create map object with types of ‘key,value’ pair values using javascript.

When we execute our program on browser we can’t see the result so we have to use that shortcut ‘ctrl+shift+j’ then result will displayed on console by console.log() method.

This method only will print any message or value on the console, for generally printing values or string on webpage in javascript we had many inbuilt methods.

We will see all these methods in future. I hope this tutorial on JavaScript key, value map helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪