DEV Community

Shubham_Baghel
Shubham_Baghel

Posted on

Object Destructuring in Javascript

Data structures in JavaScript are Object and Array.

Objects allow us to create a single entity that stores data items by key, and arrays allow us to gather data items into an ordered collection.

But when we pass those to a function, it may need not an object/array as a whole, but rather individual pieces.

Destructuring simply implies breaking down a complex structure into simpler parts. In JavaScript, this complex structure is usually an object or an array. With the destructuring syntax, you can extract smaller fragments from arrays and objects. Destructuring syntax can be used for variable declaration or variable assignment. You can also handle nested structures by using nested destructuring syntax.

const data = {firstname: 'shubham',lastname: 'B',country: 'India'}; // Object Destructuring const { firstname, lastname, country } = data; // visible, reader-editable JavaScript code goes here console.log(firstname, lastname, country); // shubham B India

Top comments (0)