Destructuring in ES6 Flashcards

1
Q

What is Destructuring in JavaScript?

A

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”];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can you use number for desctructuring?

A

You cannot use Numbers for destructuring. Numbers will throw an error because numbers cannot be variable names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can declaration and assignment can be done separately in destructuring?

A

Declaration and assignment can be done separately in destructuring.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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:

A

console.log(c); //Output: Fan
console.log(d); //Output: Rug
console.log(e); //Output: undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

var things = [“Table”, “Chair”, “Fan”, “Rug”];
var [a, b, c] = things;
console.log(c);

A

Output: Fan

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

var a, b;
[a = 40, b = 4] = [];
console.log(a);
console.log(b);

A

var a, b;
[a = 40, b = 4] = [];
console.log(a); //Output: 40
console.log(b); //Output: 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

[a = 40, b = 4] = [1, 23];
console.log(a);
console.log(b);

A

[a = 40, b = 4] = [1, 23];
console.log(a); //Output: 1
console.log(b); //Output: 23

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

var [first = “Cotlin”, second = first] = [];
console.log(first);
console.log(second);

A

var [first = “Cotlin”, second = first] = [];
console.log(first); //Output: Cotlin
console.log(second); //Output: Cotlin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

var [first = “Cotlin”, second = first] = [“Koku”];
console.log(first);
console.log(second);

A

var [first = “Cotlin”, second = first] = [“Koku”];
console.log(first); //Output: Koku
console.log(second); //Output: Koku

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

var [first = “Cotlin”, second = first] = [“Koku”, “Lydia”];
console.log(first);
console.log(second);

A

var [first = “Cotlin”, second = first] = [“Koku”, “Lydia”];
console.log(first); //Output: Koku
console.log(second); //Output: Lydia

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Destructuring lets you map a variable to the elements you are interested in. How can you ignore or skip other elements in the array?

A

You can ignore or skip the other elements in the array by using trailing commas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

var a, b;
[a, , b] = [“Lordy”, “Crown”, “Roses”];

console.log(a);
console.log(b);

A

var a, b;
[a, , b] = [“Lordy”, “Crown”, “Roses”];

console.log(a); //Output: Lordy
console.log(b); //Output: Roses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Rest parameter used for?

A

A Rest parameter is used to map all the remaining elements in the array that have not been mapped to the rest variable itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];
var [first, , third, …others] = planets;

console.log(first);
console.log(third);
console.log(others);

A

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”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If the (…) operator appears on the right-hand in destructuring then it is called a?

A

If the (…) operator appears on the right-hand in destructuring then it is a SPREAD SYNTAX

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];

var [first, second, …rest] = [“Mercury”, “Earth”, …planets, “Saturn”];

console.log(first); //Output: Mercury
console.log(second); //Output: Earth
console.log(rest); //Output: [“Venus”, “Mars”, “Pluto”, “Saturn”]

A

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];

var [first, second, …rest] = [“Mercury”, “Earth”, …planets, “Saturn”];

console.log(first); //Output: Mercury
console.log(second); //Output: Earth
console.log(rest); //Output: [“Venus”, “Mars”, “Pluto”, “Saturn”]

17
Q

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];

var [first, second, …rest] = [“Mercury”, …planets];

console.log(first); //Output: Mercury
console.log(second); //Output: Mercury
console.log(rest); //Output: [“Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”]

A

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];

var [first, second, …rest] = [“Mercury”, …planets];

console.log(first); //Output: Mercury
console.log(second); //Output: Mercury
console.log(rest); //Output: [“Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”]

18
Q

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];

var [first, second, third, fourth …rest] = [“Mercury”, “Earth”, …planets];

console.log(first); //Output: Mercury
console.log(second); //Output: Earth
console.log(third); //Output: Mercury
console.log(fourth); //Output: Earth
console.log(rest); //Output: [“Venus”, “Mars”, “Pluto”, “Saturn”]

A

var planets = [“Mercury”, “Earth”, “Venus”, “Mars”, “Pluto”, “Saturn”];

var [first, second, third, fourth …rest] = [“Mercury”, “Earth”, …planets];

console.log(first); //Output: Mercury
console.log(second); //Output: Earth
console.log(third); //Output: Mercury
console.log(fourth); //Output: Earth
console.log(rest); //Output: [“Venus”, “Mars”, “Pluto”, “Saturn”]

19
Q

var a, b;
[a, b] = [“Male”, “Female”];
[a, b] = [b, a];

console.log(a); //Output: Female
console.log(b); //Output: Male

A

var a, b;
[a, b] = [“Male”, “Female”];
[a, b] = [b, a];

console.log(a); //Output: Female
console.log(b); //Output: Male

20
Q

var numbers = [8, [1, 2, 3], 10, 12];
var [a, [d, e, f]] = numbers;

console.log(a); // Output: 8
console.log(d); // Output: 1
console.log(e); // Output: 2

A

var numbers = [8, [1, 2, 3], 10, 12];
var [a, [d, e, f]] = numbers;

console.log(a); // Output: 8
console.log(d); // Output: 1
console.log(e); // Output: 2

21
Q

var places = [“first”, “second”, “third”, “fourth”];
var [a, b, , d] = [f, …rest] = places;

console.log(a); //Output: first
console.log(d); //Output: fourth
console.log(f); //Output: first
console.log(rest); //Output: [“second”, “third”, “fourth”]

A

var places = [“first”, “second”, “third”, “fourth”];
var [a, b, , d] = [f, …rest] = places;

console.log(a); //Output: first
console.log(d); //Output: fourth
console.log(f); //Output: first
console.log(rest); //Output: [“second”, “third”, “fourth”]