Tell us what’s happening:
The code below is correct but I don’t understand why the hint said: const [b,a] = [a,b] will result in the value of a,b as undefined(simple assignment rule left to right), while [a, b] = [b, a] is correct.
The other hint said: No need of const [b,a] as it will keep the effect of assignment local, what does it mean by local? aren’t the two variables swap values in the global scope?
Your code so far
let a = 8, b = 6;
// change code below this line
[a, b] = [b, a];
// 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 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
.
Challenge: Use Destructuring Assignment to Assign Variables from Arrays
Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays