Skip to content

JavaScript map get value by key | Example code

  • by

JavaScript Map objects hold key-value pairs where values of any type can be used as either keys or values. Use the keys() method to get value by key in the JavaScript map.


JavaScript map get value by key

A simple example code uses the keys() method and gets all keys of a given object. Use this key into a loop and fetch one by one values using the get() method.

<!DOCTYPE html>
  <script >
    const obj = new Map();
    obj.set('0', 'foo');
    obj.set(1, 'bar');

    var itr = obj.keys();

    for(i=0; i<obj.size; i++)  


JavaScript map get value by key

Get an element from a map by key

If you want to see the roles of John , you use the get() method:

let userRoles = new Map([
    [john, 'admin'],
    [lily, 'editor'],
    [peter, 'subscriber']
userRoles.get(john); // admin

Do comment if you have any doubts or suggestions on this Js map topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading