Introduction
After creating a Map, JavaScript provides several built-in methods to add, update, retrieve, remove, and iterate through key-value pairs. These methods make Maps one of the most powerful and flexible data structures in JavaScript.
Unlike plain objects, Maps support keys of any data type, preserve the insertion order, and provide efficient operations for managing data.
Map methods were introduced in ES6 (ECMAScript 2015) and are widely used in modern JavaScript development, Node.js applications, API testing, and automation frameworks such as Selenium, Playwright, and Cypress.
For automation engineers, Map methods are useful for handling browser configurations, environment variables, API request headers, test data, lookup tables, and application settings.
Common Map Methods
The following are the most commonly used Map methods and properties:
| Method / Property | Description |
|---|---|
set() | Adds or updates a key-value pair. |
get() | Retrieves the value associated with a key. |
has() | Checks whether a key exists. |
delete() | Removes a key-value pair. |
clear() | Removes all entries from the Map. |
size | Returns the number of entries in the Map. |
keys() | Returns an iterator of all keys. |
values() | Returns an iterator of all values. |
entries() | Returns an iterator of all key-value pairs. |
forEach() | Executes a function for each entry. |
set() Method
The set() method adds a new key-value pair or updates the value if the key already exists.
Syntax
mapName.set(key, value);
Example
let student = new Map();
student.set("name", "John");
student.set("age", 22);
console.log(student);
Output
Map(2) {
'name' => 'John',
'age' => 22
}
Updating an Existing Key
If the key already exists, its value is replaced.
let product = new Map();
product.set("price", 500);
product.set("price", 650);
console.log(product);
Output
Map(1) {
'price' => 650
}
get() Method
The get() method retrieves the value associated with a key.
Syntax
mapName.get(key);
Example
let employee = new Map([
["name", "Alice"],
["department", "QA"]
]);
console.log(employee.get("name"));
Output
Alice
has() Method
The has() method checks whether a key exists in the Map.
Syntax
mapName.has(key);
Example
let browsers = new Map([
["Chrome", true],
["Firefox", true]
]);
console.log(browsers.has("Chrome"));
console.log(browsers.has("Safari"));
Output
true
false
delete() Method
The delete() method removes a key-value pair.
Syntax
mapName.delete(key);
Example
let cities = new Map([
["IN", "Delhi"],
["US", "New York"]
]);
cities.delete("US");
console.log(cities);
Output
Map(1) {
'IN' => 'Delhi'
}
clear() Method
The clear() method removes all entries from the Map.
Syntax
mapName.clear();
Example
let colors = new Map([
["R", "Red"],
["G", "Green"]
]);
colors.clear();
console.log(colors);
Output
Map(0) {}
size Property
The size property returns the number of key-value pairs.
Syntax
mapName.size
Example
let languages = new Map([
["JS", "JavaScript"],
["PY", "Python"],
["JAVA", "Java"]
]);
console.log(languages.size);
Output
3
keys() Method
The keys() method returns an iterator containing all keys.
let fruits = new Map([
["A", "Apple"],
["M", "Mango"]
]);
for (let key of fruits.keys()) {
console.log(key);
}
Output
A
M
values() Method
The values() method returns an iterator containing all values.
let fruits = new Map([
["A", "Apple"],
["M", "Mango"]
]);
for (let value of fruits.values()) {
console.log(value);
}
Output
Apple
Mango
entries() Method
The entries() method returns key-value pairs.
let countries = new Map([
["IN", "India"],
["US", "United States"]
]);
for (let entry of countries.entries()) {
console.log(entry);
}
Output
[ 'IN', 'India' ]
[ 'US', 'United States' ]
forEach() Method
The forEach() method executes a function for each entry.
let employees = new Map([
[101, "John"],
[102, "Alice"]
]);
employees.forEach(function(value, key) {
console.log(key + " : " + value);
});
Output
101 : John
102 : Alice
Real-World Example
Store country codes.
let countryCodes = new Map();
countryCodes.set("IN", "India");
countryCodes.set("US", "United States");
console.log(countryCodes.get("IN"));
Output
India
Another example:
Store product prices.
let products = new Map();
products.set("Laptop", 65000);
products.set("Mouse", 1200);
console.log(products.size);
Output
2
Automation Testing Example
Automation engineers frequently use Map methods to manage browser settings, request headers, environments, and test data.
Playwright Example
Store browser settings.
const config = new Map();
config.set("browser", "chromium");
config.set("headless", true);
console.log(config.get("browser"));
Output
chromium
Selenium Example
Store browser capabilities.
const capabilities = new Map();
capabilities.set("browserName", "chrome");
capabilities.set("platform", "Windows");
console.log(capabilities.has("browserName"));
Output
true
Cypress Example
Store environment configuration.
const environment = new Map();
environment.set("env", "QA");
environment.set("baseUrl", "https://example.com");
console.log(environment.size);
Output
2
API Testing Example
Store request headers.
const headers = new Map();
headers.set("Content-Type", "application/json");
headers.set("Authorization", "Bearer token");
console.log(headers.get("Content-Type"));
Output
application/json
Data-Driven Testing Example
Store login credentials.
const credentials = new Map();
credentials.set("username", "admin");
credentials.set("password", "admin123");
console.log(credentials.get("username"));
Output
admin
Common Mistakes
Using Dot Notation Instead of get()
Incorrect:
console.log(student.name);
Output:
undefined
Correct:
console.log(student.get("name"));
Expecting Duplicate Keys
let map = new Map();
map.set("id", 101);
map.set("id", 102);
console.log(map);
Output
Map(1) {
'id' => 102
}
The second value replaces the first.
Using length Instead of size
Incorrect:
console.log(map.length);
Output:
undefined
Correct:
console.log(map.size);
Best Practices
Use get() Instead of Dot Notation
Maps are accessed using get(), not property notation.
Use Meaningful Keys
Choose descriptive keys such as "browser", "baseUrl", or "Content-Type".
Use Maps for Dynamic Key-Value Data
Maps are ideal when keys are dynamic or are not limited to strings.
Iterate Using for...of or forEach()
These provide clean and readable ways to process Map entries.
Conclusion
JavaScript Map methods provide a powerful way to manage key-value data efficiently. Methods such as set(), get(), has(), delete(), clear(), keys(), values(), entries(), and forEach() allow developers to build flexible and maintainable applications.
For automation engineers, Map methods are especially valuable for managing browser configurations, API request headers, environment variables, test data, and application settings. Mastering these methods helps create clean, scalable, and efficient JavaScript automation scripts.
Frequently Asked Questions (FAQs)
Which method adds a key-value pair?
map.set(key, value);
Which method retrieves a value?
map.get(key);
How do you check whether a key exists?
map.has(key);
How do you remove a key-value pair?
map.delete(key);
Which property returns the number of entries?
Use the size property.
console.log(map.size);
Why are Map methods important in automation testing?
Automation engineers use Map methods to manage browser settings, environment variables, API headers, configuration data, lookup tables, and test information efficiently.
Key Takeaways
Maps store data as key-value pairs.
set()adds or updates entries.get()retrieves values using keys.has()checks whether a key exists.delete()removes a specific entry.clear()removes all entries.sizereturns the total number of key-value pairs.keys(),values(), andentries()return iterators.forEach()executes a function for every entry.Mastering Map methods is essential for writing efficient JavaScript and automation testing code.
