Lessons · JavaScript · object keys are text
Object keys are strings
Whatever you use as a key on a plain object becomes a string. Numbers work by accident; objects collapse to '[object Object]'.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Keying by user objects or coordinates on a plain object silently merges everything into one key. Map keeps real keys.
How to think about it
Are my keys strings, or something else? Strings and small integers as keys: object is fine. Anything else, or when you need insertion order and size: use Map.
Worked example
const o = {}; o[1] = "a"; o["1"] = "b";Same key: o is {"1": "b"}.const m = new Map(); m.set(1, "a"); m.set("1", "b");Two entries: Map keeps the types apart.Your turn
Key by an object safely.
const byPoint = new (); byPoint.set(point, value);
Solve one with the tests running
The trap
Object.keys returns strings even for numeric keys, so keys.map(k => k + 1) concatenates.
Practise object keys are text on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.