Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

PropertyDescriptor.value — the value of a data property

The `value` field holds a data property's stored value. Omitting it from an input descriptor is different from explicitly specifying `value: undefined`.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

Object.defineProperty(target, key, { value: newValue })
const descriptor = Object.getOwnPropertyDescriptor(target, key);
if (descriptor !== undefined && Object.hasOwn(descriptor, "value")) {
  const storedValue = descriptor.value;
}

PropertyDescriptor.value means the value field on an object that describes a data property, not a property on a global constructor. It corresponds to the specification's [[Value]] field and can hold any JavaScript value, including an object, a function, or undefined. writable controls whether assignment can replace the value; it does not freeze a stored object.

On an ordinary object, Object.defineProperty() defaults to undefined when it creates a data property without a value field. When updating an existing data property without changing its kind, omitting value preserves the old value, while value: undefined requests a replacement. If the existing data property is both non-configurable and non-writable, supplying a different value under SameValue (the comparison used by Object.is) throws a TypeError.

An accessor descriptor uses get and set instead of value and writable. You cannot mix fields from the two groups in an input descriptor: even { value: undefined, get: undefined } throws a TypeError. Fields inherited by the input descriptor object also count.

Object.getOwnPropertyDescriptor() returns undefined if there is no own property for the key. If it returns a descriptor, Object.hasOwn(descriptor, "value") distinguishes a data property holding undefined from an accessor property. The descriptor is not a live view: changing descriptor.value does not replace the value on the target. If the stored value is an object, the descriptor refers to that same object, not a clone.

Examples

{
  const target = { score: 7, get label() { return "ready"; } };
  const before = Object.getOwnPropertyDescriptor(target, "score");

  Object.defineProperty(target, "score", { enumerable: false });
  console.log(String(target.score));

  Object.defineProperty(target, "score", { value: undefined });
  const data = Object.getOwnPropertyDescriptor(target, "score");
  const accessor = Object.getOwnPropertyDescriptor(target, "label");

  console.log(`${Object.hasOwn(data, "value")}: ${data.value}`);
  console.log(`${Object.hasOwn(accessor, "value")}: ${target.label}`);
  console.log(String(before.value));
}

Result

7
true: undefined
false: ready
7

Omitting `value` preserves `7`; explicitly setting `value: undefined` replaces it. The `label` descriptor has no own `value` field, although reading `target.label` calls the getter and returns `ready`. The earlier descriptor `before` still contains `7`.

Related lessons

  • JavaScript · Lesson 45 · Advanced · Version: 4

Sources