Use Destructuring Assignment to Assign Variables from Arrays 1

Tell us what’s happening:
why is am i not able to swap the values.

Your code so far


let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
  const [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 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

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

This is a question of scope, done like that the effect is only local, inside the function (if you try console.log() inside the function you will see that inside it works)

So, how to make it global? Can you think of a way?

1 Like