SyntaxStudy
Sign Up
JavaScript Renaming Destructured Variables
JavaScript Beginner 3 min read

Renaming Destructured Variables

Rename on Destructure

Use key: newName to import a property under a different variable name, avoiding naming conflicts.

Example
const { data: userData, error: fetchError } = await getUser(id);
// Property "data" is now "userData", "error" is "fetchError"
// Rename + default combined
const { timeout: requestTimeout = 5000, retries: maxRetries = 3 } = config;
// Useful when two sources have same key name
const { id: userId } = user;
const { id: postId } = post;
Pro Tip

Renaming is syntax sugar — the original object is untouched.