Destructuring Summary
Destructuring extracts values from arrays and objects with defaults, renaming, nesting, and rest. Use it in function params, loops, imports, and API responses for cleaner, self-documenting code.
Destructuring extracts values from arrays and objects with defaults, renaming, nesting, and rest. Use it in function params, loops, imports, and API responses for cleaner, self-documenting code.
const { data: { users = [], total } = {}, error = null } = await api.get("/users");
for (const { id, name, role = "user" } of users) {
console.log(id, name, role);
}
When destructuring becomes deeply nested, consider extracting steps into named variables.
More in JavaScript