JavaScript Object Destructuring
JavaScript Object Destructuring
JavaScript Object Destructuring
Donate Now
(https://www.javascripttutorial.net/donation/)
If you want to learn how to destructure an array (https://www.javascripttutorial.net/javascript-array/) , you can check out the array
destructuring tutorial (https://www.javascripttutorial.net/es6/destructuring/) .
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 1/9
21/10/2022, 08:09 JavaScript Object Destructuring
let person = {
firstName: 'John',
lastName: 'Doe'
};
Prior to ES6, when you want to assign properties of the person object to variables, you typically do it like this:
ES6 introduces the object destructuring syntax that provides an alternative way to assign properties (https://www.javascripttutorial.net/javascript-
object-properties/) of an object (https://www.javascripttutorial.net/javascript-objects/) to variables:
In this example, the firstName and lastName properties are assigned to the fName and lName variables respectively.
In this syntax:
The identifier before the colon ( : ) is the property of the object and the identifier after the colon is the variable.
Notice that the property name is always on the left whether it’s an object literal or object destructuring syntax.
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 2/9
21/10/2022, 08:09 JavaScript Object Destructuring
If the variables have the same names as the properties of the object, you can make the code more concise as follows:
console.log(firstName); // 'John'
console.log(lastName); // 'Doe'
In this example, we declared two variables firstName and lastName , and assigned the properties of the person object to the
variables in the same statement.
It’s possible to separate the declaration and assignment. However, you must surround the variables in parentheses:
If you don’t use the parentheses, the JavaScript engine will interpret the left-hand side as a block and throw a syntax error.
When you assign a property that does not exist to a variable using the object destructuring, the variable is set to undefined . For
example:
console.log(middleName); // undefined
In this example, the middleName property doesn’t exist in the person object, therefore, the middleName variable is undefined .
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 3/9
21/10/2022, 08:09 JavaScript Object Destructuring
let person = {
firstName: 'John',
lastName: 'Doe',
currentAge: 28
};
console.log(middleName); // ''
console.log(age); // 28
In this example, we assign an empty string to the middleName variable when the person object doesn’t have the middleName
property.
Also, we assign the currentAge property to the age variable with the default value of 18.
However, when the person object does have the middleName property, the assignment works as usual:
let person = {
firstName: 'John',
lastName: 'Doe',
middleName: 'C.',
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 4/9
21/10/2022, 08:09 JavaScript Object Destructuring
currentAge: 28
};
console.log(middleName); // 'C.'
console.log(age); // 28
function getPerson() {
return null;
console.log(firstName, lastName);
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 5/9
21/10/2022, 08:09 JavaScript Object Destructuring
To avoid this, you can use the OR operator ( || ) to fallback the null object to an empty object:
Now, no error will occur. And the firstName and lastName will be undefined .
let employee = {
id: 1001,
name: {
firstName: 'John',
lastName: 'Doe'
};
The following statement destructures the properties of the nested name object into individual variables:
let {
name: {
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 6/9
21/10/2022, 08:09 JavaScript Object Destructuring
firstName,
lastName
} = employee;
console.log(firstName); // John
console.log(lastName); // Doe
let employee = {
id: 1001,
name: {
firstName: 'John',
lastName: 'Doe'
};
let {
name: {
firstName,
lastName
},
name
} = employee;
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 7/9
21/10/2022, 08:09 JavaScript Object Destructuring
console.log(firstName); // John
console.log(lastName); // Doe
let person = {
firstName: 'John',
lastName: 'Doe'
};
display(person);
It’s possible to destructure the object argument passed into the function like this:
let person = {
firstName: 'John',
lastName: 'Doe'
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 8/9
21/10/2022, 08:09 JavaScript Object Destructuring
};
display(person);
It looks less verbose especially when you use many properties of the argument object. This technique is often used in React.
Summary
Object destructuring assigns the properties of an object to variables with the same names by default.
https://www.javascripttutorial.net/es6/javascript-object-destructuring/ 9/9