{"id":11736,"date":"2022-07-12T17:32:05","date_gmt":"2022-07-12T17:32:05","guid":{"rendered":"https:\/\/www.htmlgoodies.com\/?p=11736"},"modified":"2022-07-12T17:32:05","modified_gmt":"2022-07-12T17:32:05","slug":"es6-destructuring-javascript","status":"publish","type":"post","link":"https:\/\/www.htmlgoodies.com\/javascript\/es6-destructuring-javascript\/","title":{"rendered":"ES6 Object Destructuring"},"content":{"rendered":"

\"Angular
\nIn recent years, deconstructed foods have become all the rage among foodies, whereby chefs break down dishes to their core idea (destructuring) and then rebuild them up from their basic components (restructuring). What does this have to do with JavaScript? As it happens, it also supports the destructuring and restructuring of Arrays and Objects. Until the release of ECMAScript 6 (ES6), the destructuring part of the equation was a lot harder to achieve than the restructuring, requiring several lines of code to accomplish. Now, the ability to destructure an Array or Object in one line of code offers a wealth of coding possibilities. In this web development tutorial, we will be concentrating on Object destructuring today; the next article will focus on mixed (Object and Array) destructuring, along with some of its more advanced uses.<\/p>\n

Looking to learn web development in an online course format? Check out our listing of some of the best HTML and web development courses<\/a>.<\/p>\n

JavaScript Destructuring Basics<\/h2>\n

The Advanced TypeScript\/ES6 Array Features<\/a> article touched on Array destructuring and compared the new ES6 syntax with that of previous versions of JavaScript (JS). As an example, we assigned the elements of an array to four separate variables:<\/p>\n

\/\/ in pre-ES6 Javascript\r\nvar ivoryKnightMembers = ['John', 'Rob', 'George', 'Steve'];\r\nvar john   = ivoryKnightMembers[0], \r\n    rob    = ivoryKnightMembers[1],\r\n    George = ivoryKnightMembers[2], \r\n    Steve  = ivoryKnightMembers[3];\r\n\r\n\/\/ using ES6 destructuring\r\nlet ivoryKnightMembers = ['John', 'Rob', 'George', 'Steve'];\r\nlet [john, rob, george, steve] = ivoryKnightMembers;\r\n<\/pre>\n

In that JavaScript tutorial, we defined destructuring as a feature of EcmaScript 2015 and Typescript that allows you to break up the structure of an entity. While that definition sufficed in the context of the subject matter at hand, it omitted a few other points about destructuring, such as:<\/p>\n