JavaScript Maps

Projected Time

30-45 minutes

Prerequisites

Motivation

Map object provided by ES6. Map is a collection of elements where each element is stored as a Key, value pair. Map object can hold both objects and primitive values as either key or value. You can use objects of all kinds as map keys. The system will not automatically convert the keys to strings as it does for object literals. This opens up a wide range of opportunities to do interesting things with maps.

Objectives

Participants will be able to:

Specific Things to Learn

Materials

Lesson

Map

Maps are a new object type that allow to store collections of key-value pairs. Unlike with objects, map keys can be of any type, even objects or functions. With maps we can iterate in the order in which the values were added, unlike objects where there’s no guarantee about the order.

Set

Sets are a new object type with ES6 (ES2015) that allow to create collections of unique values. The values in a set can be either simple primitives like strings or integers, or more complex object types like object literals or arrays.

Independent Practice

Create a map and try performing set, get, clear, delete, has etc methods on it.

Supplemental Materials