Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays

I did’nt understand how this code works to shuffle the values from a & b

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

``````

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36</code>.

**Challenge:** Use Destructuring Assignment to Assign Variables from Arrays

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays``````

Yeah, that’s weird syntax. Would this be more clear?

``````let a = 8, b = 6;
let tempArray = [b, a];
[a, b] = tempArray;
``````

We put the two values in an array and then extract them with destructuring.

This also would have worked:

``````let a = 8, b = 6;
let tempArray = [a, b];
[b, a] = tempArray;
``````
3 Likes

yup, I thought that looked an odd syntactic style too…

I pictured it as destructuring to imaginary temp vars and then restructuring them the other way around in the same way.

1 Like

Now, I got the point…Thank You

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