Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties vs. Keys vs. Values in JavaScript

Tags:

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?

like image 972
George Avatar asked Feb 21 '15 16:02

George


People also ask

Are properties and keys the same in JavaScript?

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.

Is a property a key or a value?

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.

What are keys and values in JavaScript?

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" .

What is the difference between key and value?

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.


4 Answers

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).

like image 58
Bergi Avatar answered Oct 22 '22 14:10

Bergi


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
like image 37
Niraj Regmi Avatar answered Oct 22 '22 12:10

Niraj Regmi


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

like image 45
Damian Polac Avatar answered Oct 22 '22 13:10

Damian Polac


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.

like image 45
HappyHands31 Avatar answered Oct 22 '22 12:10

HappyHands31