Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introduction to Programming Objects and Arrays Objects

Joe Williams
Joe Williams
4,014 Points

When defining the property of an object, why would you choose "employee number" over employee_number?

(If I use the wrong word when describing anything, please feel free to point out what it is actually called.)

In the most recent lesson for Javascript I watched, I learned how to give an object various properties. He did the example of how if you name an object "me", you could give it the properties "first_name" and "last_name". Additionally, calling on one of these specific properties would be me.first_name, or me.last_name.

var me = {
   first_name: "Joe", 
   last_name: "Williams"
}

Where I got confused is when he explained that while first_name and last_name are valid names, "Employee number: 1" would not be and that it would have to be a string.

var me = {
   first_name:"Joe",
   last_name:"Williams",
   "Employee number": 1;
}

Why wouldn't I just say Employee_number in this instance?

2 Answers

Jeremy Hayden
Jeremy Hayden
1,740 Points

Because it is in quotes. All he is reminding you is that if it is in quotes it is a string. And yes, the space between words would also be a problem.

Hmm... Interesting question. As far as I know it's similar but you can't put a space in the first option and you can put spaces in second. Also when you want to get that property value you would need to do this:

me["Employee number"]

I am sure there is a more complex explanation in this. Would love to see it.

Why the hell can I upvote myself? Lol