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?
Answer 5606e28586f55245dd00049a. Properties are made up of key : value pairs. We may see the term property used to indicate a key, but on the whole we may think of a property as having both a name and a value. This of course differs from key, since key is just a name.
A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything. We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It's easy to find a file by its name or add/remove a file.
An object contains properties, or key-value pairs. The desk object above has four properties. Each property has a name, which is also called a key, and a corresponding value. For instance, the key height has the value "4 feet" .
Key value pair is a convenient way of storing structured data: the key designates the kind of information (e.g. be it a name, an identifier, a URL, a path, a hash of some data etc) and value designates a piece of data of the designated kind (e.g. "John", "1247", "http://example.com/", "/data/file1.
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).
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
Property is that part of object named "name" with value 5. Key is a word "name".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With