28

I'm trying to clarify my understanding of the terms "property" vs. "keys" vs. "values" in the JavaScript realm. After reading a few books on the language and even googling the terms, I still don't feel like I'm clear on their precise meaning. So suppose we have the following:

var object = {"name" : 5};

Is my understanding of the following terms correct:

property refers to "name"

key refers to "name"

value refers to 5

I'm most concerned about "property": does it refer to identifiers only, or to the whole name/value pair?

George
  • 6,927
  • 4
  • 34
  • 67
  • You are correct. And also by property you mean key-value pair altogether, but you can refer to key only too. – dfsq Feb 21 '15 at 16:29
  • 3
    You say `key` when you are talking about `maps`. You say `property` or `attribute` when talking about an `object`. And off-course `value` should be pretty clear. – sarveshseri Feb 21 '15 at 16:30
  • The above comment is the best I have read and better than the 2 answers here so far. The thing about Javascript is that js objects and js arrays are pretty much the same thing, unlike most other languages. – 2pha Feb 21 '15 at 16:45
  • @2pha: objects are the same as associative arrays `{ }` but not as a normal array `[ ]`. In javascript there is no separate concept for an associative array, hence it is an object. – kasper Taeymans Feb 21 '15 at 16:58
  • There is a clear separate concept. real arrays have functions specific to arrays – 2pha Feb 21 '15 at 17:05
  • @kasperTaeymans: In contrast to most languages' associative arrays, JavaScript objects do not have any ordering. One should not use that term at all. – Bergi Feb 21 '15 at 17:18
  • @kasperTaeymans here is a quick fiddle to show clear difference with objects and arrays http://jsfiddle.net/2pha/yxom2a47/, and shows that like all things js, arrays are objects. – 2pha Feb 21 '15 at 17:44

4 Answers4

17

They don't have a precise meaning, especially "property" is ambiguous.

The term property (also: attribute, less common or even used for different things in JS) usually refers to the key/value pair that describes a member of an object. While, especially when used with a specific identifier (key), it often refers to the whole combination, it can also denote the value of that member. It does usually not mean the identifier itself.

When people try to be accurate, they distinguish between "property" (the whole thing, part of an object), "property name" (the string used as the key) and "property value" (the stored data).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Actually property is something different then attribute. – MaxZoom Jan 18 '17 at 19:35
  • 1
    @MaxZoom As the linked Wikipedia article puts it, "*the term attribute can and is often treated as equivalent to a property depending on the technology being discussed*". In [OOP](https://en.wikipedia.org/wiki/Class_(computer_programming)#Structure) they're synonymous, in [JS DOM](http://stackoverflow.com/q/6003819/1048572) they're absolutely not. – Bergi Jan 18 '17 at 20:28
7

This topic was really confusing for me as well. Different courses I did had different interpretations for the term "property". After consulting with different mentors I came up with this conclusion. Correct me if I'm still wrong.

name : 5 => property(key/value pair)
name => key or property name
5 => value or property value
Niraj Regmi
  • 71
  • 1
  • 1
2

Property is that part of object named "name" with value 5. Key is a word "name".

Damian Polac
  • 911
  • 9
  • 20
  • 1
    So it seems that properties refer *both* to names and values? – George Feb 21 '15 at 16:34
  • 2
    @George Yes. Also MDN have similar definition here: https://developer.mozilla.org/en-US/docs/Glossary/property Note that we still say it's the same property when value is changed. – Damian Polac Feb 21 '15 at 16:44
1

Just want to point out - I was also confused about this because of the hasOwnProperty() method, where this returns true:

const object1 = new Object();
object1.property1 = 42;
console.log(object1.hasOwnProperty('property1'));

And this returns false:

const object1 = new Object();
object1.property1 = 42;
console.log(object1.hasOwnProperty('42'));

So, while the other answers are correct, in the case of the hasOwnProperty() method, property does = the property's key.

HappyHands31
  • 4,001
  • 17
  • 59
  • 109