{"id":4321,"date":"2022-03-07T08:26:04","date_gmt":"2022-03-07T08:26:04","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4321"},"modified":"2022-03-07T08:26:05","modified_gmt":"2022-03-07T08:26:05","slug":"javascript-destructuring","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/03\/07\/javascript-destructuring\/","title":{"rendered":"Javascript &#8211; Destructuring"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>Destructuring is a feature which allows us to unpack values from arrays or properties from objects without having to write statements for them. In the sample code  in lines 5,6 , we have an example of using destructuring to use the items in the string array. Each element is given a name. You can skip certain elements also by using empty value in its places as shown in lines 8,9.<\/p>\n\n\n\n<p>A third way is to access only some values and then get the rest of the values in a single variable. This is done by prefixing the variable name with three dots as shown in lines 11,12.<\/p>\n\n\n\n<p>You can actually do away with the variable name of the array itself and directly assign names to values as shown in line 15.<\/p>\n\n\n\n<p>Using destructure syntax , we can swap values as shown in lines 17,18 and then 26,27,28.<\/p>\n\n\n\n<p>We can use the same destructuring concept for properties in Objects as shown in lines 30 onwards. However, you cannot skip values like you could do in arrays.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n&lt;html&gt;\n\t&lt;head&gt;&lt;title&gt;Destructuring&lt;\/title&gt;\n\t\n\t&lt;script&gt;\n\t   let arr = &#x5B;&quot;This&quot;, &quot;is&quot;, &quot;a&quot;, &quot;test&quot;, &quot;string&quot;];\n\t   let &#x5B;a1, a2, a3, a4,\ta5] = arr;\n\n\t   let arr2 = &#x5B;56,4,34,12];\n\t   let &#x5B;n1,,,n4] = arr2;\n\n\t   let arr3 = &#x5B;156,14,134,12];\n\t   let &#x5B;m1,...mall] = arr3;\n\n\n\t   let &#x5B;fname, lname] = &#x5B;&quot;Tom&quot;, &quot;Thumb&quot;];\n\n\t   let x = 190;\n\t   let y = 900;\n\n\t   document.write(arr&#x5B;0] + &quot;,&quot; + arr&#x5B;1] + &quot;&lt;br&gt;&quot;);\n\t   document.write(a1 +&quot;,&quot; + a2 + &quot;,&quot; + a5 + &quot;&lt;br&gt;&quot;); \n\t   document.write(n1 +&quot;,&quot; + n4 + &quot;&lt;br&gt;&quot;);\n\t   document.write(m1 +&quot;,&quot; + mall + &quot;&lt;br&gt;&quot;);\n\t   document.write(fname +&quot;,&quot; + lname + &quot;&lt;br&gt;&quot;);\n\n\t   document.write(x +&quot;,&quot; + y + &quot;&lt;br&gt;&quot;);\n\t   &#x5B;x,y] = &#x5B;y,x];\n\t   document.write(x +&quot;,&quot; + y + &quot;&lt;br&gt;&quot;);\n\n\t   let Person = {id:788, name: &quot;jim Logan&quot;, age:44};\n\t   let\t{id, name, age} = Person;\n\n\t   let Person2 = {id2:788, name2: &quot;jim Logan&quot;, age2:44};\n\t   let\t{age2} = Person2;\n\n\n\t&lt;\/script&gt;\n\t\n\t&lt;\/head&gt;\n\t&lt;body&gt;\n\t&lt;\/body&gt;\n&lt;\/html&gt;\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">This,is\n This,is,string\n 56,12\n 156,14,134,12\n Tom,Thumb\n 190,900\n 900,190<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW Destructuring is a feature which allows us to unpack values from arrays or properties from objects without having to write statements for them. In <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/03\/07\/javascript-destructuring\/\" title=\"Javascript &#8211; Destructuring\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2463,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-4321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4321"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4321\/revisions"}],"predecessor-version":[{"id":4327,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4321\/revisions\/4327"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2463"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}