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:

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

You are attempting to declare variables inside the anonymous function. a and b are already declared outside the IIFE (Immediately Invoke Function Expression), so you can simply use restructuring without creating new variables inside the IIFE. Otherwise, the globally declared a and b will be unaffected by what happens inside the IIFE.