xxxxxxxxxx
let [person, age] = ['John', 26]
let msg = `${person} is ${age} years old`
console.log(msg)
xxxxxxxxxx
//simple example with object------------------------------
let obj = {name: 'Max', age: 22, address: 'Delhi'};
const {name, age} = obj;
console.log(name);
//expected output: "Max"
console.log(age);
//expected output: 22
console.log(address);
//expected output: Uncaught ReferenceError: address is not defined
// simple example with array-------------------------------
let a, b, rest;
[a, b] = [10, 20];
console.log(a);
// expected output: 10
console.log(b);
// expected output: 20
[a, b, rest] = [10, 20, 30, 40, 50];
console.log(rest);
// expected output: Array [30,40,50]
xxxxxxxxxx
// destructuring object & nested object & combine object into single object
let user = {
name: 'Mike',
friend: ["John", "Paul", "Jimmy"],
location: {
region:"England",
country:"United Kingdom"
},
aboutMe: {
status: "Single",
pet: "Dog",
}
}
const { name, friend, location, aboutMe: {status , pet} } = user;
console.log(name); // output: "Mike"
console.log(friend); // output: [ 'John', 'Paul', 'Jimmy' ]
console.log(location); // output: { region: 'England', country: 'United Kingdom' }
console.log(status); // output: "Single"
console.log(pet); // output: "Dog"
//Combining Obj
const newUser = {
user,
car: {
make: "Buick",
year: 2012,
}
}
console.log(newUser)
// output user obj + car object into one
// {
// name: 'Mike',
// friend: [ 'John', 'Paul', 'Jimmy' ],
// location: { region: 'England', country: 'United Kingdom' },
// aboutMe: { status: 'Single', pet: 'Dog' },
// car: { make: 'Buick', year: 2012 }
// }
//Bonus destructuring from object of array
const {friend: [a, b]} = user
console.log(a) // output: "John"
console.log(b) // output: ["Paul", "Jimmy"]
xxxxxxxxxx
const hero = {
name: 'Batman',
realName: 'Bruce Wayne'
};
const { name, realName } = hero;
name; // => 'Batman',
realName; // => 'Bruce Wayne'
xxxxxxxxxx
const arr = [
{
name: 'adam',
age: 'nineteen'
}
]
const [ result ] = arr
console.log(result.name)
xxxxxxxxxx
console.log('Hello World') // Hello World
const {log} = console;
log('Hello World') // Hello World
xxxxxxxxxx
const arrValue = ['one', 'two', 'three'];
// destructuring assignment in arrays
const [x, y, z] = arrValue;
console.log(x); // one
console.log(y); // two
console.log(z); // three
xxxxxxxxxx
let a, b, rest;
[a, b] = [10, 20];
console.log(a); // 10
console.log(b); // 20
[a, b, rest] = [10, 20, 30, 40, 50];
console.log(a); // 10
console.log(b); // 20
console.log(rest); // [30, 40, 50]
({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20
// Stage 4(finished) proposal
({a, b, rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}
xxxxxxxxxx
const [a, b] = array;
const [a, , b] = array;
const [a = aDefault, b] = array;
const [a, b, rest] = array;
const [a, , b, rest] = array;
const [a, b, { pop, push }] = array;
const [a, b, [c, d]] = array;
const { a, b } = obj;
const { a: a1, b: b1 } = obj;
const { a: a1 = aDefault, b = bDefault } = obj;
const { a, b, rest } = obj;
const { a: a1, b: b1, rest } = obj;
let a, b, a1, b1, c, d, rest, pop, push;
[a, b] = array;
[a, , b] = array;
[a = aDefault, b] = array;
[a, b, rest] = array;
[a, , b, rest] = array;
[a, b, { pop, push }] = array;
[a, b, [c, d]] = array;
({ a, b } = obj); // brackets are required
({ a: a1, b: b1 } = obj);
({ a: a1 = aDefault, b = bDefault } = obj);
({ a, b, rest } = obj);
({ a: a1, b: b1, rest } = obj);
xxxxxxxxxx
let [a, b] = [9, 5]
[b, a] = [a, b]
console.log(a, b);
//Expected output: 5,9