ES6: Use Destructuring Assignment to Assign Variables from Arrays - Valid code doesn't pass?

Why does this code not pass the test?

The official solution seems to create an array without declaring it. I’ve seen this technique used before, but how can I learn more about it? Is it unique to Javascript?

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

// change code above this line
console.log(a); // should be 6
console.log(b); // should be 8
1 Like

It is a valid solution, but the FCC tests for this challenge are not robust enough to handle the extra variable *arr being used. I will see if I can modify the test to be more flexible.


It’s fairly widespread, with slight differences in what’s allowed between languages. Ones I can think of + bit of googling gives me PHP, Python, Perl, Ruby, various Lisps, Go, most functional languages (eg ML style like Haskell/SML/OCaml, or BEAM languages like Erlang/Elixir), languages that have absorbed stuff from functional languages (notably C# and Rust, Swift, Kotlin)

1 Like