Use Destructuring Assignment to Assign Variables from Arrays_question

Tell us what’s happening:
Could this be a form of destructuring?

Your code so far


let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
  (a=6,b=8);
  // 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 (X11; CrOS x86_64 12105.100.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.144 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

Or would this be a bad practice of switching values?

The problem is that you manually reassigned the values, instead of swapping a for b.
Usually in cases like that we should do something like:

let x = 10, y = 20;
[x, y] = [y, x];

That way you guarantee that the values are gonna be swapped.

1 Like