Destructuring arrays and objects

Tell us what’s happening:
Describe your issue in detail here.
I cannot understand what is meant destructuring arrays and objects, and how is it useful. And what is the difference between spread operator and array destructuring?

Your code so far


let a = 8, b = 6;
// Only change code below this line
[a, b] = [b, a];

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36

Challenge: Use Destructuring Assignment to Assign Variables from Arrays

Link to the challenge:

You just swapped the value of two variables - something that without destructuring would take a third variable.
Later lections will go into some more details, but just for changing values this is quite nice, as you don’t have to track everything and think about the order of operations.

I sometimes use it if I have like 3 or 4 variables which I have to combine into new ones.
As the destructuring is done on the spot, so if I want to add a to b, but also add (b+c)/2 to a, and double c I can do it in one line.

I don’t know how to do this task with the spread-operator, so I have trouble seeing how they are in any way similar.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.