What is the difference between Map and Object?
Answer
Both store key-value pairs, but they differ significantly. Object keys must be strings or Symbols — other types are converted to strings. Map keys can be any type (objects, functions, primitives — even NaN). Object has a prototype (inherited properties can pollute your data — use Object.create(null) for a clean dictionary). Map has no prototype keys — safer for dynamic key storage. Map maintains insertion order (so does modern Object, but it is not guaranteed by spec for all cases). Map has a built-in .size property; Object requires Object.keys(obj).length. Map is iterable (for...of, .forEach(), .entries(), .keys(), .values()). Map is more performant for frequent additions/removals. Object is better for static data with known keys and JSON serialization (JSON.stringify does not serialize Maps). Use Map when: keys are non-strings, insertion order matters, size checking is needed, or key-value pairs are frequently added/removed.