It is convenient way to extract multiple values stored from arrays and object.
It makes possible to unpack values from arrays and properties from object.

Example:-
const course = {
courseName: “JavaScript”,
duration: “Six Months”,
tutor: “Salman”
};

function india({courseName,duration, tutor}) {
console.log(courseName); //JavaScript
console.log(duration); //Six Months
console.log(tutor); // Salman
}
india(course)

Object returning from a function.

Example:-
function show(z) {
return z;
}

const x = show(course);
const { courseName, duration, tutor } = x;

console.log(courseName); //JavaScript
console.log(duration); // Six Months
console.log(tutor); // Salman

Nested Object Destructuring

Example:-
const courseObj = {
courseN: “JavaScript”,
dur: “Six Months”,
tut: “Salman”,
student: {
s1: “Ram”,
s2: “Rahim”
}
};

const { courseN, dur, tut, student: { s1,s2 }} = courseObj;
console.log(s1); // Ram
console.log(s2); // Rahim

when Destructuring array, we use their index/position in the assignment.

Example:-
const a = [10,20,30];
[x,y,z] = a;
console.log(x); //10
console.log(y); //20
console.log(z); //30

const yes = [“JavaScript”, “Six Months”, “Salman”];
const [cour, time, name] = yes;
console.log(cour); //JavaScript
console.log(time); //Six Months
console.log(name); // Salman

const [cour, time] = yes;
console.log(cour); // JavaScript
console.log(time); // Six Months

If I want cour and name only.

Examples:-
const [cour, , name] = yes;
console.log(cour); //JavaScript
console.log(name); //Salman

var p,q;
[p,q] = [10,20];
console.log(p); //10
console.log(q); //20

[p,q=20] = [10];
console.log(p); //10
console.log(q); //20

[p=40,q=20] = [10];
console.log(p); //10
console.log(q); //20

Array passing to a function.

const array = [10,20,30];

function des([v1,v2,v3]) {
console.log(v1); //10
console.log(v2); //20
console.log(v3); //30
}
des(array)

const multiValueArray = [10,20,30,40,50,60,70];

function multiple([v1,v2,v3, …rest]) {
console.log(v1); //10
console.log(v2); //20
console.log(v3); //30
console.log(rest); // [ 40, 50, 60, 70 ]
}

multiple(multiValueArray);

Array returning from a function.

const funArray = [11,12,13];

function shown(d) {
return d;
}

const x = shown(funArray);

const [value1, value2, value3] = x;
console.log(value1); // 11
console.log(value2); // 12
console.log(value3); // 13

Software Engineer. Open Source Enthusiastic