Spot Map vs Object Mistake
Question
This code tries to use an object as a map with non-string keys. What is the problem?
Code
const cache = {};
cache[true] = "yes";
cache[1] = "one";
console.log(cache["1"]);
Hint
Object keys can only be strings or symbols.
Explanation
Plain object keys are always coerced to strings. cache[true] becomes cache["true"], and cache[1] becomes cache["1"]. So cache["1"] is "one" (overwritten by the last assignment with key 1). If you need non-string keys (booleans, numbers, objects), use a Map instead: const cache = new Map(); cache.set(true, 'yes');.
Related Resources
Related Lessons
- JavaScript Data Types
Learn about Map vs Object.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.