JavaScript Basic Concepts of Map
The Map object in JavaScript is a collection of key-value pairs where the keys can be of any data type. It is similar to an object but provides more flexibility with key types. Key Features of Map Unique Keys: A Map object doesn’t allow duplicate keys. Each key is unique. Any Data Type for Keys: Unlike objects, a map’s keys can be of any type, even functions, objects, or primitive values. Order of Elements: The elements in a Map are ordered. They are iterated in the order of their insertion. Size Property: Map has a size property that returns the number of key-value pairs. Iterable: You can iterate through a Map’s keys, values, or entries using methods like keys(), values(), entries(), and for…of loops. Creating a Map const myMap = new Map(); myMap.set('name', 'John Doe'); myMap.set(42, 'The Answer'); myMap.set(true, 'boolean key'); console.log(myMap.get('name')); console.log(myMap.has(42)); // Output: true myMap.delete('name'); // Removes the key-value pair with the key 'name' for (let [key, value] of myMap) { console.log(key, value); } ...