Working with Maps Flashcards

(15 cards)

1
Q

What is a JavaScript Map?

A

A Map is a data structure that stores key-value pairs. Unlike objects, Maps allow keys to be any data type (objects, functions, primitives) and preserve insertion order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a new, empty JavaScript Map?

A

const myMap = new Map();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you initialize a Map with initial key-value pairs when creating it?

A

const myArray = [[“name”, “John”], [“age”, 30]];
const myMap = new Map(myArray);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you add a new key-value pair to a Map or update an existing value?

A

myMap.set(“city”, “New York”);
myMap.set(“age”, 31); // updates existing key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you retrieve a value from a Map using its key?

A

const cityName = myMap.get(“city”); // ‘New York’
If key doesn’t exist, .get() returns undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you check if a Map contains a key?

A

myMap.has(“name”); // true
myMap.has(“address”); // false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you remove a key-value pair from a Map?

A

myMap.delete(“city”); // true if removed, false otherwise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you remove all key-value pairs from a Map?

A

myMap.clear(); // removes all entries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you iterate through all key-value pairs in a Map?

A

for (const [key, value] of myMap) {
console.log(key, value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you iterate through all the keys in a Map?

A

for (const key of myMap.keys()) {
console.log(key);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you iterate through all the values in a Map?

A

for (const value of myMap.values()) {
console.log(value);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the significance of using objects as keys in a Map?

A

Maps allow associating data directly with objects — useful for caching or metadata storage.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you get the number of key-value pairs in a Map?

A

const mapSize = myMap.size; // returns number of entries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the key differences between Object and Map?

A
  • Object keys must be strings/symbols; Map keys can be any type.
  • Map preserves insertion order.
  • Map has .size; Object requires manual counting.
  • Map is directly iterable; Object needs Object.keys() or Object.entries().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When might you choose Map over Object?

A

When you need:
- Non-string keys (objects/functions)
- Ordered entries
- Frequent add/remove operations
- Quick access to collection size (.size property)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly