Tell us what’s happening:
My code works, but I needed the hint, and still don’t fully understand what’s going on here:
[b,a] = [a,b];
As I understand destructuring assignment, it unpacks arrays, but no array up to this point has been created. Are we creating an “anonymous array”, if there is such a thing, simply by enclosing the two declared variables in square brackets on the left of the =
?
Also, why doesn’t "use strict";
break this? Aren’t we creating a new array with the [b,a] =
code?
Thanks,
Your code so far
let a = 8, b = 6;
(() => {
"use strict";
// change code below this line
[b,a] = [a,b]
// change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays