Tutorials References Exercises Videos NEW Menu
Paid Courses Website NEW

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Let JS Const JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Events JS Strings JS String Methods JS String Search JS String Templates JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop For In JS Loop For Of JS Loop While JS Break JS Iterables JS Sets JS Maps JS Typeof JS Type Conversion JS Bitwise JS RegExp JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Arrow Function JS Classes JS JSON JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words

JS Versions

JS Versions JS 2009 (ES5) JS 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / Edge JS History

JS Objects

Object Definitions Object Properties Object Methods Object Display Object Accessors Object Constructors Object Prototypes Object Iterables Object Sets Object Maps Object Reference

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Closures

JS Classes

Class Intro Class Inheritance Class Static

JS Async

JS Callbacks JS Asynchronous JS Promises JS Async/Await

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM Forms DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

Web API Intro Web Forms API Web History API Web Storage API Web Worker API Web Fetch API Web Geolocation API

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery Selectors jQuery HTML jQuery CSS jQuery DOM

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Certificate

JS References

JavaScript Objects HTML DOM Objects


JavaScript Maps

A Map holds key-value pairs where the keys can be any datatype.

A Map remembers the original insertion order of the keys.

A Map has a property that represents the size of the map.

Map Methods

MethodDescription
new Map()Creates a new Map object
set()Sets the value for a key in a Map
get()Gets the value for a key in a Map
clear()Removes all the elements from a Map
delete()Removes a Map element specified by a key
has()Returns true if a key exists in a Map
forEach()Invokes a callback for each key/value pair in a Map
entries()Returns an iterator object with the [key, value] pairs in a Map
keys()Returns an iterator object with the keys in a Map
values()Returns an iterator object of the values in a Map
PropertyDescription
sizeReturns the number of Map elements

How to Create a Map

You can create a JavaScript Map by:

  • Passing an Array to new Map()
  • Create a Map and use Map.set()

new Map()

You can create a Map by passing an Array to the new Map() constructor:

Example

// Create a Map
const fruits = new Map([
  ["apples", 500],
  ["bananas", 300],
  ["oranges", 200]
]);
Try it Yourself »

Map.set()

You can add elements to a Map with the set() method:

Example

// Create a Map
const fruits = new Map();

// Set Map Values
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);
Try it Yourself »

The set() method can also be used to change existing Map values:

Example

fruits.set("apples", 500);
Try it Yourself »

Map.get()

The get() method gets the value of a key in a Map:

Example

fruits.get("apples");    // Returns 500
Try it Yourself »


Map.size

The size property returns the number of elements in a Map:

Example

fruits.size;
Try it Yourself »

Map.delete()

The delete() method removes a Map element:

Example

fruits.delete("apples");
Try it Yourself »

Map.clear()

The clear() method removes all the elements from a Map:

Example

fruits.clear();
Try it Yourself »

Map.has()

The has() method returns true if a key exists in a Map:

Example

fruits.has("apples");
Try it Yourself »

Try This:

fruits.delete("apples");
fruits.has("apples");
Try it Yourself »

Maps are Objects

typeof returns object:

Example

// Returns object:
typeof fruits;
Try it Yourself »

instanceof Map returns true:

Example

// Returns true:
fruits instanceof Map;
Try it Yourself »

JavaScript Objects vs Maps

Differences between JavaScript Objects and Maps:

ObjectMap
Iterable Not directly iterable Directly iterable
Size Do not have a size property Have a size property
Key Types Keys must be Strings (or Symbols) Keys can be any datatype
Key Order Keys are not well ordered Keys are ordered by insertion
Defaults Have default keys Do not have default keys

Map.forEach()

The forEach() method invokes a callback for each key/value pair in a Map:

Example

// List all entries
let text = "";
fruits.forEach (function(value, key) {
  text += key + ' = ' + value;
})
Try it Yourself »

Map.keys()

The keys() method returns an iterator object with the keys in a Map:

Example

// List all keys
let veggies = "";
for (const x of fruits.keys()) {
  veggies += x;
}
Try it Yourself »

Map.values()

The values method returns an iterator object with the values in a Map:

Example

// Sum all values
let total = 0;
for (const x of fruits.values()) {
  total += x;
}
Try it Yourself »

Map.entries()

The entries() method returns an iterator object with the [key,values] in a Map:

Example

// List all entries
let text = "";
for (const x of fruits.entries()) {
  text += x;
}
Try it Yourself »

Objects as Keys

Being able to use objects as keys is an important Map feature.

Example

// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};

// Create a Map
const fruits = new Map();

// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);
Try it Yourself »

Remember: The key is an object (apples), not a string ("apples"):

Example

fruits.get("apples");  // Returns undefined
Try it Yourself »

Browser Support

JavaScript Maps are supported in all browsers, except Internet Explorer:

Chrome Edge Firefox Safari Opera