Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
To create a new Map, you can use the `Map()` constructor. Here’s an example:
const myMap = new Map();
You can also pass an array of key-value pairs to the `Map()` constructor to initialize the Map:
const myMap = new Map([
To add a new key-value pair to a Map, you can use the `set()` method:
To retrieve a value from a Map, you can use the `get()` method:
const value = myMap.get('key1');
console.log(value); // Output: 'value1'
You can also check if a key exists in a Map using the `has()` method:
const hasKey = myMap.has('key1');
console.log(hasKey); // Output: true
To remove a key-value pair from a Map, you can use the `delete()` method:
Finally, you can get the number of key-value pairs in a Map using the `size` property:
const size = myMap.size;
console.log(size); // Output: 2