{
"$type": "site.standard.document",
"content": {
"$type": "site.standard.content.markdown",
"text": "I read a very interesting article today by Andres Reales about JavaScript object keys. I was previously under the impression that you could use numbers as object keys in JavaScript. Andres's article highlights why this is a misconception, and exposes the truth about JavaScript objects: every key is a string.\n\n```js\nconst object = {\n\t42: 'the answer to life, the universe, and everything'\n};\n\nconsole.log(object);\n// {42: 'the answer to life, the universe, and everything'}\n\nconsole.log(object[42]);\n// 'the answer to life, the universe, and everything'\n```\n\nAll looks good until now... but here things get a bit odd\n\n```js\nconsole.log(object.42);\n// Uncaught SyntaxError: Unexpected number\n\nconsole.log(object.'42');\n// Uncaught SyntaxError: Unexpected number\n\nconsole.log(object['42']);\n// 'the answer to life, the universe, and everything'\n\nconsole.log(Object.keys(object));\n// ['42']\n\nconst fortyTwoStr = '42';\nconst fortyTwoNum = 42;\n\nobject[fortyTwoNum]\n// 'the answer to life, the universe, and everything'\n\nobject[fortyTwoStr]\n// 'the answer to life, the universe, and everything'\n```\n\nYou learn something every day.\n\nhttps://www.becomebetterprogrammer.com/can-javasscript-object-keys-be-numbers-or-non-string-values/",
"version": "1.0"
},
"description": "As it turns out, there are quite a few tricks that JavaScript plays on us all. One of those is pretending things are something they are not.",
"path": "/blog/javascript-object-keys",
"publishedAt": "2022-10-06T18:10:00.000Z",
"site": "https://joeinn.es",
"textContent": "I read a very interesting article today by Andres Reales about JavaScript object keys. I was previously under the impression that you could use numbers as object keys in JavaScript. Andres's article highlights why this is a misconception, and exposes the truth about JavaScript objects: every key is a string.\n\nAll looks good until now... but here things get a bit odd\n\nYou learn something every day.\n\nhttps://www.becomebetterprogrammer.com/can-javasscript-object-keys-be-numbers-or-non-string-values/",
"title": "Every Key Is A String"
}