Javascript dictionary key types
In Javascript, the dictionary type coerces its keys to Strings:
z = { 3: "three", 4: "four" }
for (var x in z) { console.log(typeof x); }
string
This sort of makes sense because dictionaries are really just Javscript objects, and objects are typically accessed by strings. Ie: z.foo is the same thing as z[“foo”]. Worth noting that z.3 doesn’t work, though. It gets spookier when you use null:
z[null] = "foo"
for (var x in z){ console.log(x, typeof x); }
3 string
4 string
null string
Maybe it wasn’t wise to use integers and null everywhere as keys in my JSON dictionaries. In fact the JSON spec says keys have to be strings, and the Python JSON library was already serializing my keys as strings (with None mapped to “null”). Now I know.