Reputation: 7317
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?
Upvotes: 29
Views: 9919
Reputation: 1
In JavaScript, an objects consists of key-value pairs, known as properties
.
but it could also be property name(key)
and property value(value)
.
Upvotes: 0
Reputation: 4101
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.
Upvotes: 1
Reputation: 71
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
Upvotes: 7
Reputation: 664548
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).
Upvotes: 18
Reputation: 934
Property is that part of object named "name" with value 5. Key is a word "name".
Upvotes: 2