DEV Community

Creating and Manipulating Objects in JavaScript

Boniface Gordian on August 07, 2024

Introduction JavaScript objects are essential tools for developers, providing a versatile way to structure and manage data. Objects allo...
Collapse
 
jonrandy profile image
Jon Randy 🎖️

...where each key is a string...

Unfortunately this is not correct. Keys can also be Symbols

Collapse
 
greenersoft profile image
GreenerSoft

Or an integer and a Boolean. This is in JSON where the keys are strings.

Collapse
 
boniface_gordian profile image
Boniface Gordian

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!

Collapse
 
jonrandy profile image
Jon Randy 🎖️

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.

Thread Thread
 
greenersoft profile image
GreenerSoft

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.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

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:

With it, you can do stuff like this:

// Getting
const arr = ['a', 'b', 'c']
const obj = {x: 3, y: 6, z: 9}
const str = "wxyz"

arr[[0, 2]]   // ['a', 'c']
obj[['y', 'z']]   // [6, 9]
str[[1, 3]]   // "xz"

// Setting
arr[[0, 2]] = ['p', 'q']  // arr is now ['p', 'b', 'q']
obj[['y', 'z']] = [2, 1]   // obj is now {x: 3, y: 2, z: 1}
// No setting for strings, since they are immutable
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
greenersoft profile image
GreenerSoft

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.

Thread Thread
 
boniface_gordian profile image
Boniface Gordian

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!

Collapse
 
boniface_gordian profile image
Boniface Gordian

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!

Collapse
 
alex_mills_fe1096fc0621c4 profile image
Alex mills • Edited

Creating and manipulating objects in JavaScript is fundamental for effective web development. Understanding how to define and interact with objects allows for dynamic and responsive programming. As businesses expand, announcing these updates through a Business Expansion Press Release can be an excellent way to keep your audience informed and engaged, showcasing new features and improvements.

Collapse
 
greenersoft profile image
GreenerSoft

Welcome ChatGPT 🤣