Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Object destructuring

Object destructuring reads properties by key rather than by position. Use it to declare variables, assign to existing targets, provide defaults, or collect remaining properties.

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

Syntax

const { name, theme: colorTheme = "light", ...rest } = profile;
let score;
({ points: score } = profile);
const { [key]: value, settings: { mode } = {} } = options;
function readName({ name = "Guest" } = {}) { return name; }

Object destructuring uses a pattern in {} to read properties by key. In const { name } = profile, the variable name gets the value of profile.name. The pattern { name: displayName } reads the same key into a variable called displayName; it does not rename the property. The pattern { [key]: value } computes the key, while { settings: { mode } } reads a nested property and binds only mode.

In a const, let, or var declaration or a function parameter, the pattern creates bindings. The assignment ({ points: score } = profile) writes to the existing variable score; a writable property can also be a target. In a standalone statement, the parentheses prevent the opening { from being parsed as the start of a block.

In { theme = "light" }, the default expression is evaluated only if the property read returns undefined, for example when the property isn't found. Values such as null, false, and 0 are kept. Applying an object pattern to null or undefined itself throws a TypeError, even in a nested pattern. In function readName({ name = "Guest" } = {}), the parameter default = {} handles an omitted argument or undefined, but not null.

An explicit key lookup can find an inherited property. In contrast, ...rest makes a shallow copy of only the remaining own enumerable properties, including symbol-keyed ones, into a new object. Object values are still shared. Put ...rest last, with no trailing comma.

This is ECMAScript 2024 syntax, not a browser API. For older JavaScript runtimes, check support for the forms you use, especially ...rest.

Examples

{
  const profile = { name: "Mira", theme: undefined, badge: null, points: 7 };
  const { name: displayName, theme = "light", badge = "none", ...rest } = profile;
  let score;
  ({ points: score } = profile);
  console.log(`${displayName} | ${theme} | ${badge} | ${score} | ${Object.keys(rest).join(",")}`);
}

Result

Mira | light | null | 7 | points

`displayName` receives the value of `name`, `theme` falls back to its default, and `badge` stays `null`. The assignment writes `7` to `score`; `points` is the only key in `rest`.

Related lessons

  • JavaScript · Lesson 39 · Advanced · Version: 4
  • JavaScript · Lesson 5 · Basic · Version: 4
  • JavaScript · Lesson 18 · Basic · Version: 4
  • JavaScript · Lesson 2 · Intermediate · Version: 3
  • JavaScript · Lesson 8 · Intermediate · Version: 3
  • JavaScript · Lesson 10 · Intermediate · Version: 3

Sources