Sub topic 2
Unpacking Objects by Property Name
This chapter assumes you can declare variables with let and const, work with plain objects, and read array destructuring from the previous chapter. Array destructuring matches values by position. Object destructuring matches values by property name.
The Basic Pattern
An object pattern is a list of property names inside curly braces on the left side of an assignment. Each name creates a variable and looks up the property with the same name on the right-hand object.
const user = { name: 'Ada', id: 7, active: true };
const { name, active } = user;
console.log(name); // 'Ada'
console.log(active); // trueThis code is shorthand for two separate lookups:
const user = { name: 'Ada', id: 7, active: true };
const name = user.name;
const active = user.active;
console.log(name, active); // 'Ada' trueYou only list the properties you want. The id property is ignored here, and nothing has to stand in its place. Array patterns need empty slots to skip elements, but object patterns don't.
Order Does Not Matter
JavaScript finds each property by its key, so the order in the pattern has no effect on the result.
const point = { x: 3, y: 8 };
const { y, x } = point;
console.log(x, y); // 3 8Missing Properties Become undefined
If the object has no property with the requested name, the variable gets undefined. No error is thrown.
const config = { port: 8080 };
const { port, host } = config;
console.log(port); // 8080
console.log(host); // undefinedThis behavior causes one of the most common bugs with this syntax. A typo in the pattern fails silently:
const response = { status: 200, body: 'ok' };
const { stauts } = response; // typo
console.log(stauts); // undefined, not 200A later chapter, "Supplying Default Values", shows how to replace undefined with a fallback value.
Lookups Follow Normal Property Access
Destructuring reads properties the same way the dot operator does. That has two consequences you might not expect:
- Inherited properties are found on the prototype chain.
- Primitive values such as strings are temporarily wrapped in an object, so their properties can be read.
const { toString } = {};
console.log(typeof toString); // 'function' (inherited from Object.prototype)
const { length } = 'hello';
console.log(length); // 5The exceptions are null and undefined, which have no properties at all. Destructuring either one throws a TypeError. The chapter "When Defaults Don't Apply and Destructuring Throws" covers this case.
Variable Names Must Be Valid Identifiers
The shorthand form uses the property name as the variable name. That works only if the key is a valid identifier. A key like 'first-name' or 'class' can't become a variable directly. The next chapters show how to give the variable a different name.
| Pattern | Object | Result |
|---|---|---|
| `{ a }` | `{ a: 1 }` | `a` is `1` |
| `{ b }` | `{ a: 1 }` | `b` is `undefined` |
| `{ length }` | `'abc'` | `length` is `3` |
| `{ a }` | `null` | `TypeError` |
Exercise
Given const book = { title: 'Dune', year: 1965, pages: 412 };, what does const { pages, author, title } = book; console.log(title, author, pages); print, and why?
Show answer
It prints Dune undefined 412. Properties are matched by name, so the order of the pattern does not matter. book has no author property, so author is undefined and no error is thrown.
Exercise
Why does const { size } = new Set([1, 2, 3]); give size the value 3, even though size is not an own property of the set?
Show answer
Destructuring reads properties the same way set.size does. size is a getter defined on Set.prototype, and that lookup walks the prototype chain, so it returns 3.