Object Destructuring in ES6

Object Destructuring in ES6

The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values are discarded from an array. On the other hand, in object destructuring, the keys (properties) and comparable values can be discarded from an object.

When we work with object destructuring, we consider keys or values as the variable names. The property name of the object and the variable name must be the same in object destructuring. If they don’t have any match, it returns an undefined value. In the object destructuring, we can extract the values with the help of keys instead of their index (position).

Example

Here, we have an example to define the simple assignment in object destructuring.

const value = {p: 200, q: 400};
const {p, q} = value;
console.log(p);
console.log(q);   

Output

After the successful execution of the above code, we got the following output:

Object Destructuring in ES6

Here, we are going to discuss the basic assignment of object destructuring.

Example

Const employee ={name: “Jacob”, position: “Manager”, age: “28”};
Const {name, position, age} = employee;
Console.log(name);
Console.log(position);
Console.log(age);

Output

After the execution of the above example, we got the following output:

Object Destructuring in ES6

Default values and Object Destructuring

In object destructuring, a default value can be allotted to a variable if the value discarded from the object like array destructuring.

Example

We will try to understand above concept with the help of following example.

const {a = 50, b = 100} = {a = 200};
console.log(a);
console.log(b);

Output

After the execution of the code, we got the following output:

Object Destructuring in ES6

In the above example, we have defined two variables (a, b), which havedefault values 50 and 100. Then, we reassigned the value of variable a (200). We did not reassign the value to variable b. So,the variable b holds its original value.

Assignment of New variable name

Unlike the property of an object, we can easily assign a variable with various names.

Example

Here, we have discussed the above concept with the help of the following example.   

const number = {r: 20, s: 25};
const {r: new1, s: new2} = number;
console.log(new1);
console.log(new2);

Output

After the execution of the above example, we got the following:

Object Destructuring in ES6

Assignment without Declaration                                                        

If we did not assign the value of a variable during the variable declaration, we can easily assign it at the time of destructuring.  

Example

Here, we have an example to understand the above concept.     

let name, age;
({name, age} = {name: “Stuart”, age: “30”}) ;
Console.log(name);
Console.log(age);                    

Output

After the execution of the above code, we got the following output:

Object Destructuring in ES6

Rest operator with Object Destructuring

In Object Destructing, the rest parameter (…)  is used to store the persisting keys of an object into a new object variable.

Example

Here, we have discussed the above concept with the help of following example:

let {u, v, …args} = {u: 10, v:20, w: 30, x: 40, y: 50}
console.log(u);
console.log(v);
console.log(args);  

Output

After the execution of the code, we got the following output:

Object Destructuring in ES6

Assigning Variable Name and Providing Default Values

We can assign a discarded property of an object to a variable with a different name. In the object destructuring, we can assign the default values to the variable, if the discarded value is undefined.  

Example

Here, we have an example to understand the above concept.

const {l:number1=25, m: number2=35} = {l: 45};
console.log(number1);
console.log(number2);

Output

After the execution of code, we got the following output:

Object Destructuring in ES6

Related Topics

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.