Articles → JAVASCRIPT → Array And Object Destructuring In Javascript

Array And Object Destructuring In Javascript






Purpose





Example Of Array Destructuring


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

Try It



Picture showing the output of array destructuring in javascript



Example Of Object Destructuring


<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>

Try It

Picture showing the output of object destructuring in javascript



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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250