Destructuring objects and arrays
Pull values out in one line, with defaults and new names.
JavaScript · Destructuring objects and arraysBeginner
const user = { id: 7, name: 'Ali', address: { city: 'Lahore' } };
const { name, role = 'guest', address: { city } } = user; // role gets a default
const { id: userId } = user; // rename
const [first, , third = 'none'] = ['a', 'b']; // skip one, default for third
function show({ name, age = 18 }) { // in function parameters
return `${name} (${age})`;
}destructuringobjectarrayes6