Destructuring Array Problem

Tell us what’s happening:
I’m doing this problem on destructuring array and i can’t run the code if i declare [b, a] with const. I know a , b are declare before but we didn’t define array so why cant we define it with const. PLEASE HELP!!

Your code so far

let a = 8, b = 6;
// Only change code below this line
[b, a] = [a, b];

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0.

Challenge: Use Destructuring Assignment to Assign Variables from Arrays

Link to the challenge:

const [b, a] = [a, b]

In a destructuring assignment like this, any variables on the left side of the = are being declared. That’s how the syntax works.

[b, a] = [a, b]

But here, none of the variables are being declared. Its an all or nothing.

Yeah but the 2nd case in your answer is correct not the first one.

… yes… because in the first example the variables are being re-declared. That’s how the syntax works.

1 Like

Thanks for the help. I got it now.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.