liveBook · Manning

Default value

As explained in JavaScript in Depth

The attribute values JavaScript assigns to a property descriptor when the defining syntax does not specify them.

A surprising difference appears when defining the same property two ways: object literals enable writable, enumerable, and configurable by default, while Object.defineProperty() disables all three unless specified. section 5.2.3

Use these defaults as a practical rule:

  • Object literal properties default to writable: true, enumerable: true, and configurable: true. section 5.2.3
  • Object.defineProperty() and Object.defineProperties() default writable, enumerable, and configurable to false when omitted. section 5.2.3
  • Writable controls value changes; configurable controls deletion and later descriptor changes; enumerable controls whether listing APIs expose the property. section 5.2.3

The same-looking property, different behavior

Object.getOwnPropertyDescriptor() reveals the difference: a property created as { prop: 'value' } reports all three attributes as true, whereas one created with Object.defineProperty(obj, 'prop', { value: 'value' }) reports all three as false.

How JavaScript in Depth covers it

How deep it goes
Defines three defaults, contrasts creation syntaxes, and distinguishes attribute effects without discussing engine optimization internals.
Examples
Compares descriptor inspection for an object-literal property with inspection for a property created by Object.defineProperty().
What it assumes
Assumes basic JavaScript objects and properties while distinguishing descriptor attributes explicitly.
What you can do after
Diagnose unexpected property behavior by inspecting writable, enumerable, and configurable with Object.getOwnPropertyDescriptor().
How it connects
Connects default attributes with property descriptors, Object.defineProperty(), Object.defineProperties(), object literals, and Object.keys().

Also explained in

Compare how 23 books explain default value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.