xxxxxxxxxx
const arr = [
{
name: 'adam',
age: 'nineteen'
}
]
const [ result ] = arr
console.log(result.name)
xxxxxxxxxx
//destructuring array
const alphabet = ['a', 'b', 'c', 'b', 'e'];
const [a, b] = alphabet;
console.log(a, b);
//Expected output: a b
xxxxxxxxxx
//Without destructuring
const myArray = ["a", "b", "c"];
const x = myArray[0];
const y = myArray[1];
console.log(x, y); // "a" "b"
//With destructuring
const myArray = ["a", "b", "c"];
const [x, y] = myArray; // That's it !
console.log(x, y); // "a" "b"
xxxxxxxxxx
const foo = ['one', 'two', 'three'];
const [red, yellow, green] = foo;
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // "three"
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
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
xxxxxxxxxx
let cars = ['ferrari', 'tesla', 'cadillac'];
let [car1, car2, car3] = cars;
console.log(car1, car2, car3); // Prints: ferrari tesla cadillac
xxxxxxxxxx
let [person, age] = ['John', 26]
let msg = `${person} is ${age} years old`
console.log(msg)