Skip to content

Object Destructuring

Object destructuring offers a sleek way to extract some variables from an object.

Here's a quick example:

const user = {
name: 'François Bouchard',
city: 'Saint-Louis-du-Ha! Ha!',
province: 'Québec',
country: 'Canada',
postalCode: 'A1B 2C3',
};
const { name, country } = user;
console.log(name); // ‘François Bouchard’
console.log(country); // ‘Canada’

This is effectively the same thing as doing this:

const name = user.name;
const country = user.country;

We can pluck out as many or as few values as we want.

Renaming extracted values

Consider this situation:

const name = 'Hello!';
const user = { name: 'Marie-Hélene Pelletier' };
const { name } = user;
// Uncaught SyntaxError:
// Identifier 'name' has already been declared

We tried to destructure the name property into its own variable, but we run into an issue: there's already a variable called name!

In these cases, we can rename the value as we unpack it:

const name = 'Hello!';
const user = { name: 'Marie-Hélene Pelletier' };
const { name: userName } = user;
console.log(name); // ‘Hello!’
console.log(userName); // ‘Marie-Hélene Pelletier’

Default values

Here's a question: what happens if we try to destructure a key from an object which isn't defined?

const user = { name: 'Marie-Hélene Pelletier' };
const { status } = user;
console.log(status); // ???

Well, user.status isn't defined, and so status will be set to undefined.

If we want, we can set a default value using the assignment operator:

const { status = 'idle' } = user;

if the user object has a status property, that value will be plucked out and assigned to the new status constant. Otherwise, status will be assigned to the string "idle".

In order words, it's a modern version of this:

const status = typeof user.status === 'undefined'
? 'idle'
: user.status;

Destructuring function parameters

Let's suppose we have a function which takes an object as its first parameter:

function validateUser(user) {
if (typeof user.name !== 'string') {
return false;
}
if (user.password.length < 12) {
return false;
}
return true;
}

If we wanted, we could destructure these values at the top of the function:

function validateUser(user) {
const { name, password } = user;
if (typeof name !== 'string') {
return false;
}
if (password.length < 12) {
return false;
}
return true;
}

Using parameter destructuring, we can do this destructuring right in the function parameters:

function validateUser({ name, password }) {
if (typeof name !== 'string') {
return false;
}
if (password.length < 12) {
return false;
}
return true;
}

All 3 of these code snippets are equivalent, but many developers enjoy using function parameter destructuring. It's especially popular in React to destructure the props in our components.

Default parameter values

Like with typical object destructuring, we can supply default values to be used for parameters.

Here's a quick example:

function sendApiRequest({ method = 'GET', numOfRetries }) {
// Stuff
}

When I call this function, I can supply my own value for method:

sendApiRequest({ method: 'PUT', numOfRetries: 4 });

…Or, if I want it to be equal to GET, I can omit it entirely:

sendApiRequest({ numOfRetries: 4 });