Destructuring in ES6 Flashcards
What is Destructuring in JavaScript?
Destructuring in JavaScript is a simplified method of extracting multiple properties from an array by taking the structure and deconstructing it down into its own constituent parts through assignments by using a syntax that looks similar to array literals.
var [first, second, third] = [“Laide”, “Gabriel”, “Jets”];
Can you use number for desctructuring?
You cannot use Numbers for destructuring. Numbers will throw an error because numbers cannot be variable names.
Can declaration and assignment can be done separately in destructuring?
Declaration and assignment can be done separately in destructuring.
var things = [“Table”, “Chair”, “Fan”, “Rug”];
var [a, b, c, d, e] = things;
console.log(c); //Output:
console.log(d); //Output:
console.log(e); //Output:
console.log(c); //Output: Fan
console.log(d); //Output: Rug
console.log(e); //Output: undefined
var things = [“Table”, “Chair”, “Fan”, “Rug”];
var [a, b, c] = things;
console.log(c);
Output: Fan
var a, b;
[a = 40, b = 4] = [];
console.log(a);
console.log(b);
var a, b;
[a = 40, b = 4] = [];
console.log(a); //Output: 40
console.log(b); //Output: 4
[a = 40, b = 4] = [1, 23];
console.log(a);
console.log(b);
[a = 40, b = 4] = [1, 23];
console.log(a); //Output: 1
console.log(b); //Output: 23
var [first = “Cotlin”, second = first] = [];
console.log(first);
console.log(second);
var [first = “Cotlin”, second = first] = [];
console.log(first); //Output: Cotlin
console.log(second); //Output: Cotlin
var [first = “Cotlin”, second = first] = [“Koku”];
console.log(first);
console.log(second);
var [first = “Cotlin”, second = first] = [“Koku”];
console.log(first); //Output: Koku
console.log(second); //Output: Koku
var [first = “Cotlin”, second = first] = [“Koku”, “Lydia”];
console.log(first);
console.log(second);
var [first = “Cotlin”, second = first] = [“Koku”, “Lydia”];
console.log(first); //Output: Koku
console.log(second); //Output: Lydia
Destructuring lets you map a variable to the elements you are interested in. How can you ignore or skip other elements in the array?
You can ignore or skip the other elements in the array by using trailing commas.
var a, b;
[a, , b] = [“Lordy”, “Crown”, “Roses”];
console.log(a);
console.log(b);
var a, b;
[a, , b] = [“Lordy”, “Crown”, “Roses”];
console.log(a); //Output: Lordy
console.log(b); //Output: Roses
What is a Rest parameter used for?
A Rest parameter is used to map all the remaining elements in the array that have not been mapped to the rest variable itself.
var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];
var [first, , third, …others] = planets;
console.log(first);
console.log(third);
console.log(others);
var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];
var [first, , third, …others] = planets;
console.log(first); //Output: Mercury
console.log(third); //Output: Venus
console.log(others); //Output: [“Mars”, “Pluto”, “Saturn”]
If the (…) operator appears on the right-hand in destructuring then it is called a?
If the (…) operator appears on the right-hand in destructuring then it is a SPREAD SYNTAX