Introduction
JavaScript objects are essential tools for developers, providing a versatile way to structure and manage data. Objects allo...
For further actions, you may consider blocking this person and/or reporting abuse
Unfortunately this is not correct. Keys can also be
Symbol
sOr an integer and a Boolean. This is in JSON where the keys are strings.
Thank you for your comment! You’re right to highlight the difference between JavaScript objects and JSON, where keys must be strings. I’ve updated the article to clarify this distinction. I’m also planning to publish a more detailed article on JavaScript objects, where I’ll dive deeper into these concepts. I'm looking forward to sharing more insights with the community. Thanks again for engaging with the content!
Keys cannot be integers or Booleans, and this post has nothing to do with JSON. If you try to use anything other than a string or a symbol as an object key, it will be coerced to a string.
Yes, but :
let a = {1: "A", 2: "B"} or let b = {true: "C", false: "D"} are objects that can be written in JavaScript (but not in JSON).
And a[1] gives "A" and b[true] gives "C".
I agree that internally these keys are converted to strings, but JavaScript allows you to write them as integers or Booleans and access them as such.
Only because they're coerced to strings both on setting and getting the properties. It's completely wrong to say that Object keys can be anything other than strings or symbols.
I'm also still confused as to why you're bringing JSON into the this, since the post is not concerned with JSON at all - but you are correct that keys in JSON must always be quoted strings.
You can have some fun with property accessors though - I wrote a library to allow the use of Arrays as property accessors:
Turboprop: JS Arrays as Property Accessors!?!
Jon Randy 🎖️ ・ May 26 '22
With it, you can do stuff like this:
I agree that internally properties are only strings or symbols [ref], but I'm talking in terms of defining an object in JavaScript: you can use integers, booleans, expressions ([]), functions, getters, setters, etc. for properties.
For JSON, this is to remind beginners that it's not the same thing.
Thank you for your comment! You’re right to highlight the difference between JavaScript objects and JSON, where keys must be strings. I’ve updated the article to clarify this distinction. I’m also planning to publish a more detailed article on JavaScript objects, where I’ll dive deeper into these concepts. I'm looking forward to sharing more insights with the community. Thanks again for engaging with the content!
Thank you, Jon, for pointing out the important detail about JavaScript object keys being strings or symbols. I’ve updated the article to reflect this, and I appreciate your insight. I’m also working on a detailed article that will cover JavaScript objects more comprehensively, including their keys and other best practices. Keep an eye out for it—I'll be sharing more in-depth content soon. Thanks again for your feedback!