Start with the question your data needs to answer
An array is a good default when you need a list, especially if duplicates or positions matter. An object is often a good fit for a small, fixed group of named properties, such as a user’s name and email. Set and Map are useful when the job is more specific: a Set keeps a collection of unique values, while a Map associates keys with values.
You can try every example here in the console of a browser that supports ES2015 JavaScript, or in a Node.js environment with Set and Map support. No package or build tool is required. The examples use console.log so you can see the result immediately.
Use Set when membership and uniqueness matter
Suppose a form collects tags, but a tag should appear only once. You can write const tags = new Set(["html", "css", "html"]); and then console.log([...tags]); to get ["html", "css"]. Set removes the repeated value. You can add another tag with tags.add("javascript"), check for one with tags.has("css"), and remove one with tags.delete("html"). Its size property tells you how many values remain.
Set is also useful for tracking which items have already been seen. For example, while processing a list of IDs, a seenIds Set can answer seenIds.has(id) before you handle the next item. This makes the intent clearer than repeatedly searching an array. If you need a list again, [...seenIds] creates an array. A Set iterates in insertion order, but it does not provide array positions or array methods such as map. Keep an array when duplicates, indexes, or a sequence of repeated events are meaningful.
Use Map when keys point to values
A Map works well when you need to look up a value by a key that your program creates or receives. For example, const scores = new Map(); scores.set("Ada", 8); scores.set("Lin", 5); console.log(scores.get("Ada")); prints 8. Use scores.has("Ada") to check whether the key exists, scores.delete("Lin") to remove an entry, and scores.size to count entries. Calling set again with an existing key replaces its value.
Unlike ordinary object property names, Map keys can be values of many types, including numbers and objects. That can be helpful when the key is itself a particular object: const visits = new Map(); const page = { title: "Home" }; visits.set(page, 3); console.log(visits.get(page)); prints 3. Map also iterates in insertion order, so for (const [name, score] of scores) can visit its entries directly. Choose Map when you are managing a changing collection of key–value entries, rather than describing an entity with known fields.
Choose between Map, Set, arrays, and objects
Ask what each item represents. If you need only to know whether a value is present, use Set. If each key has associated information, use Map. If you need an ordered list in which the same value may occur several times, use an array. If you need named properties on a record, such as { title: "Home", published: true }, an object is usually easier to read.
For example, a reading app might use an array for the order of articles, a Set for bookmarked article IDs, and a Map from article IDs to reading progress. These collections can work together; you do not need to pick one structure for the entire app. Choosing by task also makes your code easier to explain: bookmarkedIds.has(id) communicates a membership check, while progressById.get(id) communicates a lookup.
Watch for identity and missing entries
Set and Map compare objects by identity, not by their contents. The two expressions { id: 1 } and { id: 1 } create different objects. As a result, a Set can contain both, and a Map lookup using a newly created { id: 1 } will not find an entry stored under the first object. Reuse the same object reference when it is the key, or use a stable primitive key such as an ID string when that better matches your data.
Finally, Map.get(key) returns undefined when a key is missing, but a key can also deliberately store undefined. If that distinction matters, check map.has(key) first. These details are good reasons to start with a small example and test the operations your program actually needs before replacing an existing array or object.



