Set & Map (ES6)
Set & Map (ES6): Array → Set deduplication + Map key-value mapping ES6 Set and Map fill the gaps where plain arrays and objects fall short.
Array → Set deduplication + Map key-value mapping
ES6 Set and Map fill the gaps where plain arrays and objects fall short. Set is a collection of unique values — it never accepts duplicates. Map is an advanced key-value store where keys can be any type (not just strings). They are the direct equivalents of Java's HashSet and HashMap. Think of a Set as the name badges at an event: try to write the same name twice and the list quietly swallows it. A Map is the cloakroom ticket — whatever you hand over (a number, an object, even a function), you get the matching coat back. So why do we need these two when plain arrays and plain objects already exist? Two concrete reasons: first, a plain object ALWAYS coerces its keys to strings — `obj[1]` and `obj['1']` point at the same box; second, searching an array with `includes()` rescans from the start every time, whereas `Set.has()` returns in constant time. The trade-off you learned between `HashSet` and `ArrayList.contains()` in Java transfers exactly. For QA the most practical use is duplicate detection: dropping a list of order numbers into a Set and comparing its size against the original length answers "are we creating the same record twice?" in a single line — one of the most expensive bug classes in payment and signup flows.
Set & Map — QA Automation Examples
// ─── SET ───────────────────────────────────────────────────── // Java equivalent: Set set = new HashSet<>(); const errorSet = new Set(); errorSet.add("ElementNotFound"); errorSet.add("TimeoutError"); errorSet.add("ElementNotFound"); // Duplicate — ignored! console.log("Error count:", errorSet.size); // 2 (no duplicates) console.log("Set contains?", errorSet.has("TimeoutError")); // true // Deduplicate an array (most common Set use case) const rawLogs = ["INFO", "ERROR", "INFO", "WARN", "ERROR"]; const uniqueLogs = [...new Set(rawLogs)]; console.log("Unique logs:", uniqueLogs); // ['INFO','ERROR','WARN'] // ─── MAP ───────────────────────────────────────────────────── // Java equivalent: Map map = new HashMap<>(); const statusMap = new Map(); statusMap.set('/api/login', 200); statusMap.set('/api/logout', 200); statusMap.set('/api/admin', 403); console.log("Login status:", statusMap.get('/api/login')); // 200 console.log("Total routes:", statusMap.size); // 3 // Iterate over Map (Java: entrySet().forEach()) for (const [url, status] of statusMap) { const icon = status === 200 ? '✅' : '❌'; console.log(`${icon} ${url} → ${status}`); }
Micro Lab: JavaScript QA coding practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Step by Step: JavaScript QA coding practice
Read the goal and async dependencies
Place await / Promise chain in the right spot
Complete the assertion or expectation line
Run and read console or test output
If flaky, add a wait strategy or retry
What is the safe order for writing and testing JavaScript QA code?
🎬 How a Set Swallows Duplicates and a Map Preserves Order