Articles → JAVASCRIPT → Array And Object Destructuring In Javascript

Array And Object Destructuring In Javascript






Purpose





Example Of Array Destructuring


<html>
   <head>
   </head>
   <body>
      <script>
         var arr = ["element1", "element2"];
         console.log(arr);
         
         
         //  Array destructuring
         var [arr1, arr2] = ["element1", "element2"];
         console.log(arr1);
         console.log(arr2);
      </script>
   </body>
</html>




Picture showing the output of array destructuring in javascript

Click to Enlarge


Example Of Object Destructuring


<html>
   <head>
   </head>
   <body>
      <script>
         const obj = {
              name: "gyan",
             country: "india"
         
          };
         
          //  The name of the variable should be same as the variables inside the object
          const { name, country } = obj;
         
          console.log("name:" + name);
          console.log("country:" + country);
      </script>
   </body>
</html>


Picture showing the output of object destructuring in javascript

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, August 25, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250