Destructuring in JavaScript

The destructuring assignment syntax is a JS expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Object Destructuring
let's see with examples:
function index(){
function user(){
let a = 20;
console.log(a);
}
function userID(){
let b = 40;
console.log(b);
}
function micro(){
let c = 80;
console.log(c);
}
return {user, userID, micro}
}
let {user, userID, micro} = index()
user()
userID()
micro()
//ans
20
40
80
Points are if you want to access more than one property in the object method we have access to an index we want to know the last name as well so, if you want to access multiple properties you just need to put a comma & mention another property name, so this way you can extract multiple properties just putting a comma and mention your properties name in object.
function index(){
function user(){
let a = 20;
console.log(a);
}
function userID(){
let b = 40;
console.log(b);
}
function micro(){
let c = 80;
console.log(c);
}
return {user, userID, micro}
}
let {user:z, userID, micro} = index()
z()
userID()
micro()
//ans
20
40
80
Sometimes we don't want to use my property with the same name because of any reason maybe the name is very big or maybe the name or the key so in that case sometimes you want to give it a different variable name that we also call creating an alias so if you want to want to give it your key or object key a different name then you can create an alias for this key and to use just need to use colon so now here instead of using this first name key with the name of 'z'.
Array Destructuring
let's see with examples:
function index(){
function user(){
let a = 20;
console.log(a);
}
function userID(){
let b = 40;
console.log(b);
}
function micro(){
let c = 80;
console.log(c);
}
return [user, userID, micro]
}
let z = index()
z[0]()
z[1]()
z[2]()
//ans
20
40
80
Points are if you want to access more than one property in the array method we have access to an Index we want to know the last name as well so, if you want to access multiple properties you just need to put a comma & mention another property name, so this way you can extract multiple properties just putting a comma and mention your properties name in object.



