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