{"id":7983,"date":"2021-08-16T12:59:18","date_gmt":"2021-08-16T12:59:18","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7983"},"modified":"2022-04-13T16:30:44","modified_gmt":"2022-04-13T16:30:44","slug":"why-doesnt-this-javascript-destructuring-assignment-work","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/","title":{"rendered":"Why doesn&#8217;t this JavaScript destructuring assignment work?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Destructuring<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Destructuring is a convenient way of extracting multiple values from data stored in objects and Arrays. The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ex:- var a = [10, 20, 30];<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">[x, y, z] = a;<br>console.log(x) \/\/ 10<br>console.log(y) \/\/ 20<br>console.log(z) \/\/ 30<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Left-hand side of the assignment to define what values to unpack from the sourced variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Array Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/5b54ffbea90f7ce37bcac45e287ab1bc.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/9afab76fd67062a828cb0ccce8eba117.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">When destructuring Array, we use their index\/positions in the assignment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Array Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/add8abfb3fc906992ba2ed13e38ba95d.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/14375d0030f439e41a2730fc16883f4d.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Array Passing to a Function<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/ccff26f75631e45d86f95589af2aafc6.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Array Returning from a Function<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/cfe228f3d339c9b95b1fc62734417f5b.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Nested Array Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/d465f792a3bed67ab7065b5d3033847e.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/5baf288560c4fe7f6361fd7f9d58a34a.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">When destructuring objects, we use the keys as variable names. This is how JavaScript knows which property of the object we want to assign.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/2d2339a31677ef488a8ac83ae43a662e.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/73b03e6e84d092aadb298ec89b1f0ce1.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/5e862a5be56e46bad6103daa97a914ab.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/c07505dd8846e8ea6cd1b88b263afed5.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/ade802e13e938f1988ca5c8d67f343cc.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/5522c89ee11d77b998c06dd783aea0a5.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/e59ee99562f25bca6d45bf6926303fb7.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Passing to a Function<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/1522b48a8e14141f8dbd04cd214a5bb1.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Object Returning from a Function<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/4d98f35d542e1191709e1db7999d655c.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Nested Object Destructuring<\/strong><\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/5ba9f27aff8f4565eabd6d2bb3a59915.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Destructuring Destructuring is a convenient way of extracting multiple values from data stored in objects and Arrays. The destructuring assignment [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[6344,6341,5095,424,6343,6342],"class_list":["post-7983","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-array-destructuring","tag-destructuring","tag-javascript","tag-nested","tag-object-destructuring","tag-object-passing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Why doesn&#039;t this JavaScript destructuring assignment work? - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Why doesn&#039;t this JavaScript destructuring assignment work? - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Destructuring Destructuring is a convenient way of extracting multiple values from data stored in objects and Arrays. The destructuring assignment [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-16T12:59:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:44+00:00\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"Why doesn&#8217;t this JavaScript destructuring assignment work?\",\"datePublished\":\"2021-08-16T12:59:18+00:00\",\"dateModified\":\"2022-04-13T16:30:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/\"},\"wordCount\":154,\"commentCount\":0,\"keywords\":[\"Array Destructuring\",\"Destructuring\",\"Javascript\",\"nested\",\"Object Destructuring\",\"Object Passing\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/\",\"name\":\"Why doesn't this JavaScript destructuring assignment work? - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-08-16T12:59:18+00:00\",\"dateModified\":\"2022-04-13T16:30:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/why-doesnt-this-javascript-destructuring-assignment-work\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Why doesn&#8217;t this JavaScript destructuring assignment work?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Why doesn't this JavaScript destructuring assignment work? - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/","og_locale":"en_US","og_type":"article","og_title":"Why doesn't this JavaScript destructuring assignment work? - scmGalaxy","og_description":"Destructuring Destructuring is a convenient way of extracting multiple values from data stored in objects and Arrays. The destructuring assignment [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/","og_site_name":"scmGalaxy","article_published_time":"2021-08-16T12:59:18+00:00","article_modified_time":"2022-04-13T16:30:44+00:00","author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"Why doesn&#8217;t this JavaScript destructuring assignment work?","datePublished":"2021-08-16T12:59:18+00:00","dateModified":"2022-04-13T16:30:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/"},"wordCount":154,"commentCount":0,"keywords":["Array Destructuring","Destructuring","Javascript","nested","Object Destructuring","Object Passing"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/","name":"Why doesn't this JavaScript destructuring assignment work? - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-08-16T12:59:18+00:00","dateModified":"2022-04-13T16:30:44+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/why-doesnt-this-javascript-destructuring-assignment-work\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Why doesn&#8217;t this JavaScript destructuring assignment work?"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7983"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7983\/revisions"}],"predecessor-version":[{"id":8005,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7983\/revisions\/8005"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}