Property (JavaScript)

A JavaScript property is a member of an object that associates a key with a value. A JavaScript object is a data structure that stores a collection of properties.

A property consists of the following parts:

  • A name (also called a key), which is either a string or a symbol.
  • A value, which can be any JavaScript value. A property that has a function as its value may also be called a method.
  • Some attributes, which specify how the property can be read and written. A property may have the configurable, enumerable, and writable attributes.

Accessor properties do not have an actual "value". The value is represented indirectly through a pair of functions, one (the getter) invoked when reading the value and one (the setter) invoked when setting the value. However, accessor properties behave like regular data properties on the surface, because the getter and setter functions are invoked automatically and are typically transparent to JavaScript code.

The property's value (including the getter and setter) and its attributes are stored in a data record called the property descriptor. Many methods, such as Object.getOwnPropertyDescriptor() and Object.defineProperty(), work with property descriptors.

The term property itself does not correspond to any JavaScript value — it's an abstract concept. For example, in the following code:

const obj = {
  a: 1,
  b() {},

The object obj has two properties. The first one has "a" as the key and 1 as the value. The second one has "b" as the key and a function as the value (using the method syntax). The "a"1, "b"function associations are the properties of the object.

In the context of classes, properties can be divided into instance properties, which are owned by each instance, and static properties, which are owned by the class and hold data common to all instances. In the context of inheritance, properties can also be divided into own properties, which are owned by the object itself, and inherited properties, which are owned by objects in the prototype chain of the object.

For more information about reading and writing properties, see working with objects.

See also