Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does object destructuring extract properties into variables?

Quick answer

Object destructuring reads properties from an object into variables. By default, each variable has the same name as its property: const { id } = user. To use a different variable name, write const { name: displayName } = user. Add = value for a default; it applies when the property is missing or its value is undefined, but not when its value is null. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a JavaScript object and want to pull a few values into variables. How does the basic form work?

Geez

Write an object pattern inside braces on the left of a declaration. By default, each property name maps to a variable with that same name. For example:

const user = { id: 42, name: "Mina" };
const { id, name } = user;

After this, id is 42 and name is "Mina". The variable names come from the property names in the destructuring pattern, not from the values. You can include only the properties you want to extract. [S1]

Follow-up question

What if I want the property name in a variable called displayName?

Geez

Use a colon to pair the property name on the left with your chosen variable name on the right:

const { name: displayName } = user;

This reads user.name into displayName; it does not declare a variable named name from that pattern. You can mix direct extraction and aliases:

const { id, name: displayName } = user;

With the example object above, id is 42 and displayName is "Mina". Read name: displayName as “take the name property and assign it to displayName.” [S1]

Follow-up question

How do I supply a fallback when a setting is missing? Does that also cover a setting explicitly set to undefined or null?

Geez

Put = fallback after the variable name in the pattern. The default is used if the property is missing or its value is undefined; it is not used for null. [S1]

const settings = { theme: undefined, volume: null };
const { theme = "light", volume = 50, language = "en" } = settings;

The resulting values are theme === "light", volume === null, and language === "en". The default therefore cannot distinguish a missing property from an existing property whose value is undefined: both produce the default. If that distinction matters, separately check whether the object owns the property, for example with Object.hasOwn(settings, "theme"). Destructuring alone does not make that distinction. [S1]

Follow-up question

Can you show the full pattern and what I should expect each variable to contain?

Geez

Here is a worked example combining direct extraction, an alias, and defaults:

const profile = {
  id: 7,
  name: "Ari",
  nickname: undefined,
  note: null,
};

const {
  id,
  name: displayName,
  nickname = "Guest",
  language = "en",
  note = "No note",
} = profile;

The resulting values are id: 7, displayName: "Ari", nickname: "Guest", language: "en", and note: null. The undefined nickname and absent language property use their defaults, while null remains null. [S1]

To read a pattern, identify the source property, the variable receiving it, and any default. In name: displayName, the source property is name and the variable is displayName. In nickname = "Guest", the property and variable share a name, and the default applies when the accessed value is undefined. These examples assume the declared variable names are available in the surrounding scope.

A practical checklist

  • Start with the source object, such as const { id } = user.

  • Use the property name for a directly matching variable.

  • Write propertyName: variableName to give the variable another name.

  • Add = fallback for a missing or undefined value.

  • Remember that a default does not replace null.

  • Use Object.hasOwn() separately if you need to distinguish an absent own property from one set to undefined.

A few more useful answers

Does destructuring with a default change the original object?

No. The pattern assigns the accessed value to a variable; it does not assign the fallback back to the source property.

Can destructuring read a property that the object inherits?

Yes. Property access during object destructuring follows the prototype chain, so an inherited property can be read. [S1]

Does a default distinguish an absent property from one set to `undefined`?

No. The same default is used in both cases. Check ownership separately if that difference matters. [S1]

Sources

References used for this guide.

  1. [S1] Destructuring — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work