Lesson: Destructure an Array

Tell us what’s happening:
This test wants me to destructure an array, but an array does not exist. I’m assuming this means I’m supposed to put a and b into an array myself.

I do this in the same line I am destructuring. Is this not allowed for some reason? Why does this lesson want me to destructure an array that doesn’t exist? My code matches the code in the instructions exactly but is clearly failing at some point, but there isn’t enough information in the lesson to explain why.

Your code so far


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

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

Almost there, but if you use const the effect will stay local. Don’t use const

1 Like

Edit: Replacing const with var does not work for some reason

let a = 8, b = 6;
(() => {
  "use strict";
  // change code below this line
  var [a, b] = [b, a];
  // change code above this line
})();
console.log(a); // should be 6
console.log(b); // should be 8

fails

Final edit: There should be no declaration at all.

Because a and b already exist and should be global, do not use the const keyword to declare them. The task here does not actually create an array. Instead you’re containing using the variables in (temporary) arrays to be able to use destructuring assignment.

1 Like

Hi Ariel, thanks for your answer, it’s helping me as well. But can you explain the following statement a bit more:

Instead you’re containing using the variables in (temporary) arrays to be able to use destructuring assignment

What makes these arrays temporary?

I can see the advantage of using an array for a fast swap of values. I’ve just never considered a temporary array before.

They only exists for the duration of the function.

Right, I do understand what temporary means. I’m actually looking for more context around the use of them.

  1. I looked at what MDN had to say on this and there was very little information other than saying it could be done.

  2. I Googled “temporary array” and found one article saying the array on the right side of the equal sign was the temporary one.

Would you mind providing further uses for temporary arrays or perhaps point me toward some MDN docs?

There’s no special concept of “temporary arrays”. Those are just arrays in variables that don’t have a very long scope, thus they’re temporary.

In this case, I don’t see why you’re using an IIFE. The purpose of those is to create an isolated scope, which is exactly what you do not want for this challenge. Remove the IIFE and just write the swapping code inline without any var/const/let.

the IIFE (and the thread) are from last version of the curriculum, when it was necessary to create a closed scope for the strict mode to be used. as for now the challenge has been changed and no more unnecessary IIFE