destructuring in javascript
destructuring in javascript :
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Example :: first:: Object
const myObjet = {
name: "Harry",
className: "first",
subject:"Hindi",
state:"Rajasthan",
pincode:300000,
}
const {name,className, subject,asd,pincode} = myObjet;
console.log(name,asd,pincode);
Output::Harry undefined 300000
Example::Second:: Object
const myObjext = {
name:"Harry",
className: "five",
subject:"Hindi",
state:"rajasthan",
pincode:123123
}
function showObject({name, className, subject,state,pincode}){
const message = "my name is "+name + " I am study in "+className +
" i like " + subject +" subject most. my state is "+ state +" ok."
console.log(message);
}
showObject(myObjext)
Output::my name is Harry I am study in five i like Hindi subject most. my state is rajasthan ok.
Example::Third:: Array
const x = [1, 2, 3, 4, 5];
const [y, z] = x;
console.log(y); // 1
console.log(z); // 2
Comments
Post a Comment