Sub topic 4
Renaming Properties as You Destructure
Object destructuring normally creates a variable with the same name as the property it reads. That works until the property name is already taken, is not a legal variable name, or simply does not suit your code. Renaming lets you read a property under one name and bind it to a variable with a different name, all inside the same pattern.
This chapter assumes you can declare variables with const and let, work with plain objects, and write basic object destructuring such as const { name } = user. It also assumes you have seen default values in patterns ({ role = 'guest' }), because renaming and defaults are often combined.
The Syntax: Property on the Left, Variable on the Right
A renaming pattern has the form { propertyName: variableName }. The part before the colon says which property to read. The part after the colon says which variable to create.
const user = { name: 'Ada', id: 7 };
const { name: userName, id: userId } = user;
console.log(userName); // 'Ada'
console.log(userId); // 7Read the pattern left to right as "take name, put it in userName". The object literal { name: 'Ada' } uses the same colon syntax, where the key comes first and the value second. A pattern mirrors that shape: the key comes first, and the "value" slot holds the place where the value will be stored.
Shorthand { name } is short for { name: name }. Renaming is the full form, and the shorthand is a special case of it.
Why Rename?
Avoiding name collisions
When you destructure two objects that share property names, the second const would redeclare the same variable. Renaming keeps both values.
const user = { id: 7, name: 'Ada' };
const order = { id: 1001, total: 59.5 };
const { id: userId } = user;
const { id: orderId, total } = order;
console.log(userId, orderId, total); // 7 1001 59.5The same issue comes up when a property name matches a variable or import already in scope, such as a name you already have or a status from an outer function.
Handling keys that are not valid identifiers
Shorthand only works when the property name is also a valid variable name. Keys with hyphens or spaces, and keys that are reserved words, have to be renamed.
const response = {
user_id: 42,
'display-name': 'Ada L.',
class: 'admin'
};
const {
user_id: userId,
'display-name': displayName,
class: role
} = response;
console.log(userId); // 42
console.log(displayName); // 'Ada L.'
console.log(role); // 'admin'{ class } is a syntax error, because class cannot be a variable name. { class: role } is allowed, because the left side is a property name, and property names may be reserved words. Quoted keys like 'display-name' also need the quotes on the left side.
Translating naming conventions
APIs often return snake_case keys, while JavaScript code usually uses camelCase. Renaming at the point of destructuring converts the names at the edge of your code, so the rest of it never sees the foreign style. user_id: userId above is an example of this.
Choosing clearer local names
A generic property name can be vague inside a function. Renaming data to invoices or value to temperatureCelsius makes the code easier to read without changing the source object.
Combining Renaming with Defaults
A default goes after the new variable name: { property: variable = defaultValue }. The default applies when the property's value is undefined, as with any other default.
const settings = { theme_name: undefined, font_size: 14 };
const {
theme_name: theme = 'light',
font_size: fontSize = 16,
line_height: lineHeight = 1.5
} = settings;
console.log(theme); // 'light' (value was undefined)
console.log(fontSize); // 14 (value present, default ignored)
console.log(lineHeight); // 1.5 (property missing)The order is fixed. { theme_name = 'light': theme } is a syntax error. Keep in mind that the default belongs to the variable, so it follows the variable.
Computed Property Names
When the property to read is only known at runtime, put an expression in square brackets on the left side. A computed key must be renamed, because there is no fixed name to reuse as the variable.
function pick(obj, key) {
const { [key]: value = null } = obj;
return value;
}
const contact = { email: '[email protected]', phone: '555-0100' };
console.log(pick(contact, 'email')); // '[email protected]'
console.log(pick(contact, 'fax')); // nullThe expression inside the brackets can be any expression, such as a template literal, a function call, or a symbol.
const lang = 'fr';
const labels = { label_en: 'Save', label_fr: 'Enregistrer' };
const { [`label_${lang}`]: saveLabel } = labels;
console.log(saveLabel); // 'Enregistrer'Numeric Keys and Array-Like Sources
Object patterns read properties from any value, including arrays. Array indexes are property keys, so you can rename them directly. This is handy when you want an element and a property such as length in one pattern.
const tags = ['js', 'css', 'html'];
const { 0: firstTag, length: tagCount } = tags;
console.log(firstTag); // 'js'
console.log(tagCount); // 3Reading the Same Property Twice
A pattern may list the same property more than once. Each entry creates its own binding. You rarely need this, but it is useful when you want both the shorthand name and a renamed copy, or when you destructure the property's contents in a second entry.
const record = { id: 7, meta: { created: '2026-01-05' } };
const { id, id: recordId, meta, meta: { created } } = record;
console.log(id, recordId); // 7 7
console.log(meta); // { created: '2026-01-05' }
console.log(created); // '2026-01-05'The meta: { created } entry uses a nested pattern in place of a variable name. Nested patterns get their own chapter later. For now, notice that the right side of the colon is a target, and a variable name is the simplest kind of target.
Renaming in Function Parameters
Renaming works the same way inside a parameter pattern. A function can accept an object with an API-style key and use a clearer name internally.
function greet({ first_name: firstName = 'stranger' }) {
return `Hello, ${firstName}!`;
}
console.log(greet({ first_name: 'Lin' })); // 'Hello, Lin!'
console.log(greet({})); // 'Hello, stranger!'Calling greet() with no argument would throw, because there is no object to destructure. A later chapter on parameter destructuring shows how to add a default for the whole object.
Common Errors
Expecting the original name to exist
Renaming creates only the new variable. The property name on the left is not bound.
const person = { fullName: 'Grace Hopper' };
const { fullName: displayName } = person;
console.log(displayName); // 'Grace Hopper'
try {
console.log(fullName);
} catch (err) {
console.log(err.name); // 'ReferenceError'
}If you need both names, list the property twice, as shown earlier.
Reversing the two sides
Writing { userName: name } when you meant { name: userName } reads a property called userName, which usually does not exist. The result is a silent undefined, with no error thrown. When a renamed variable is unexpectedly undefined, check the order before anything else.
Mixing up patterns and literals
In an object literal, { a: b } stores the value of b under key a. In a pattern, { a: b } reads key a and stores it in b. In both cases the key is on the left. What changes is the direction the data flows.
Quick Reference
| Pattern | Reads property | Creates variable | Notes |
|---|---|---|---|
| `{ name }` | `name` | `name` | Shorthand |
| `{ name: userName }` | `name` | `userName` | Basic rename |
| `{ name: userName = 'Anon' }` | `name` | `userName` | Rename plus default |
| `{ 'first-name': firstName }` | `first-name` | `firstName` | Quoted key must be renamed |
| `{ class: cls }` | `class` | `cls` | Reserved word key must be renamed |
| `{ [key]: value }` | value of `key` | `value` | Computed key must be renamed |
| `{ 0: head }` | `0` | `head` | Numeric key, works on arrays |
Exercises
Exercise
Given const payload = { 'user-name': 'kai', created_at: undefined };, write one const declaration that creates userName from 'user-name' and createdAt from created_at, with createdAt defaulting to 'unknown'. What are the two values?
Show answer
const { 'user-name': userName, created_at: createdAt = 'unknown' } = payload; gives userName === 'kai' and createdAt === 'unknown'. The default applies because created_at holds undefined.
Exercise
A teammate writes const { title: heading } = article; and then console.log(title);. What happens, and why?
Show answer
It throws a ReferenceError, unless some other variable named title is in scope. The pattern creates only heading. The left side of the colon names the property to read and does not declare a variable.
Exercise
Write a function getField(obj, field) that uses a computed key in a destructuring pattern to return the property named by field, or 'n/a' when the value is undefined. What does getField({ a: 1 }, 'b') return?
Show answer
function getField(obj, field) { const { [field]: result = 'n/a' } = obj; return result; }. The call getField({ a: 1 }, 'b') returns 'n/a'.
Exercise
Why does const { class } = { class: 'x' }; fail when const { class: c } = { class: 'x' }; works?
Show answer
Shorthand declares a variable with the property's name, and class is a reserved word that cannot be a variable name. In the renamed form, class is used only as a property key, which may be a reserved word, and the variable is c.