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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Object Literal

David Diehr
David Diehr
16,457 Points

Are keys and properties the same thing?

I'm having an incredibly difficult time understanding these videos about objects. I think my problem in the main is that the nomenclature is full of very common words that half the time I can't tell when a word is being used as part of normal speech or as the technical term, but I digress. In this video Dave says "objects contain property-value pairs or key-value pairs". Is the "or" referencing two different types of pairs that can be in objects, or does that "or" just mean that these pairs are referred to in these two ways?

5 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi David Diehr

Sorry for the confusion. Yes I use property and key interchangeably. You'll see them referred to as "properties" most of the time, but the word key does pop up too. For example, you can retrieve an object's property names using the Object.keys() method:

var person = {
  name: 'Dave',
  job: 'teacher',
  topic: 'JavaScript',
  location: 'Portland'
};
var propertyNames = Object.keys(person); // propertyNames contains an array
console.log(propertyNames.join(', ')); // outputs "name, job, topic, location"

Hope that helps!

David Diehr
David Diehr
16,457 Points

So after doing a little research and re-watching a few videos after this one with an eye to these two possibilities I'm pretty sure Dave meant something like "Objects contain property-value pairs, which are also called key-value pairs." I don't think mikes02 answer works because this video is introducing objects and he's giving us our first definition. Objects within arrays aren't discussed until a later video, and nothing about new nomenclature is mentioned there. I searched for a simple definition of "object" in javascript (surprisingly impossible to find) but the tutorial pages I found seemed to either use either "property" or "key" almost exclusively. Which leads me to believe these are two ways of saying the same thing. I'd appreciate a confirmation from anybody that can give me a definite answer.

David - I think you may be right, it sounds like he may just be using the two terms interchangeably based on how you have described the video progression.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

Oh, I didn't necessarily assume that he meant an array. I misinterpreted. Yes, I think that they basically refer to the same thing (can be interchangeable in function if not in name) but I thought he was saying that in the context of an object you'd just call it a property. Like in the context of an object you call a function a method... or like with argument and parameter. It sounded plausible. I can't check out the video until payday (my account is paused). Anyway, good luck finding a simple object definition. :)

http://radar.oreilly.com/2014/05/what-it-really-means-when-people-say-everything-in-javascript-is-an-object.html

The key actually is the name of the property.

I would think that the key-value expression would be related to an array of objects, for example:

var arr = [{key1: value1}, {key2: value2}];

Where the property-value may be more directed at an object like:

var name = {firstName:"John", lastName:"Doe"};

I haven't done this particular course yet but perhaps that is what is being suggested. Let me know if that helps you down the right path.

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

I can't see the video at the moment but I think that mikes02 is correct.

var obj = {
    keyValuePair: { key: value },
    property: value
};