Maps – Basics

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Maps – Basics

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does new Map() create a new Map object?
A Yes
B No
2. Does set() add a key-value pair to a Map?
A No
B Yes
3. Does get() read a value from a Map using its key?
A Yes
B No
4. Is 'map' with a lowercase m valid for creating a Map?
A Yes
B No
5. Must 'set' be written in lowercase?
A Yes
B No

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1new Map() Aages.set("Tom", 30)
2scores.set("Sam", 90) Bscores.get("Sam")
3Reads the value stored under a given key CWrong - 'Map' must start with a capital M
4Adds a new key-value pair to a Map DStores 90 under the key "Sam" in the scores Map
5let m = new map(); EWrong - 'set' must start with a lowercase s
6m.Set("a", 1); FCreates a new, empty Map

Definitions

new Map()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let scores = ___ Map();
2.
scores.___("Sam", 90);
3.
console.log(scores.___("Sam"));
4.
let ages = ___ Map();
5.
ages.___("Tom", 30);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A let m = new map();
B m.set("a", 1);
C console.log(m);
Question 2
A let m2 = new Map();
B m2.Set("b", 2);
C console.log(m2);
Question 3
A let m3 = new Map();
B m3.set("c", 3);
C console.log(m3.Get("c"));
Question 4
A let a = 1;
B let b = 2;
C let m5 = new map();
Question 5
A let m6 = new Map();
B m6.set("d", 4);
C console.log(m6);
D m6.Set("e", 5);

Answer Key

Maps – Basics
Section A: Quick Questions
Q1
A — Yes
Q2
B — Yes
Q3
A — Yes
Q4
B — No
Q5
A — Yes
Section B: Match the Pairs
Q1
new Map() → Creates a new, empty Map
Q2
scores.set("Sam", 90) → Stores 90 under the key "Sam" in the scores Map
Q3
Reads the value stored under a given key → scores.get("Sam")
Q4
Adds a new key-value pair to a Map → ages.set("Tom", 30)
Q5
let m = new map(); → Wrong - 'Map' must start with a capital M
Q6
m.Set("a", 1); → Wrong - 'set' must start with a lowercase s
Section C: Definitions
Q1
new Map() — Creates a new Map, a built-in object that stores key-value pairs.
Section D: Fill in the Blanks
Q1
let scores = new Map();
Q2
scores.set("Sam", 90);
Q3
console.log(scores.get("Sam"));
Q4
let ages = new Map();
Q5
ages.set("Tom", 30);
Section E: Spot the Error
Q1
A — 'Map' must start with a capital M, not lowercase.
Q2
B — 'set' must be lowercase - a capital S causes an error.
Q3
C — 'get' must be lowercase - a capital G causes an error.
Q4
C — 'Map' must start with a capital M, not lowercase.
Q5
D — 'set' must be lowercase - a capital S causes an error.