O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.1007/978-1-4302-6107-0_11

11. Working with Maps

Russ Ferguson and Keith Cirkel2
(1)
Ocean, New Jersey, USA
(2)
London, UK
 

What Are the Advantages of Using a Map over an Object?

Problem

When is it better to use a map rather than an object?

Solution

While there are similarities to maps and objects, maps can contain both objects and primitives as keys or values.

The Code

var mapObj = new Map();
    mapObj.set('myString', 'myString is a string key');
    console.log(mapObj.get('myString')); //myString is a string key
var myObj = {};
    mapObj.set(myObj, 'Object is a used as a key');
    console.log(mapObj.get(myObj)); //Object is a used as a key
Listing 11-1.
Maps Can Use Different ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required