SyntaxStudy
Sign Up
JavaScript Beginner 4 min read

Object Destructuring

Object Destructuring

Object destructuring matches by property name. Rename variables with key: alias, set defaults with = value.

Example
const { name, age } = user;
const { name: userName, role = "user" } = user; // rename + default
// Nested
const { address: { city, zip } } = user;
// All remaining
const { id, ...rest } = user; // rest = all except id
// In function params
function render({ title, className = "default" }) { }
Pro Tip

Combine rename and default: const { theme: { color: c = "blue" } = {} } = config.