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

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 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 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.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 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.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 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.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 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.

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.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 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.

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.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 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.

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.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.