Destructuring Assignment to Assign Variables from Arrays

Tell us what’s happening:
When i execute this code it throws error saying ‘a is not defined’. Basically I am not able to access the global variables a and b from inside the function. Can you please help me see what I am missing here. Thanks.!

Your code so far

let a = 8, b = 6;
(() => {
    "use strict";
    // change code below this line   
    const arr = [a,b];     
    const [b,a] = arr;
    // 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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:

Where are you seeing this error above? I am not seeing any error messages like that. Your solution is not correct and you are failing some tests.

FYI - As soon as you declare a and b in that second line, you are no longer changing the global variables. You have created local variables to the anonymous function.

HINT: Do not declare a or b as variables inside the anonymous function.

Thanks very much I see my error. I took out the const declaration and it works fine. Thanks for your help randelldawson.

    const [b,a] = arr;

By using the keyword const you are declaring locally scoped versions of a and b. Now JavaScript gets weird in some (ok, many) ways. Essentially, it knows that a and b will are declared inside the function scope even though it is on a later line and therefore it will not look for them in the global scope. When you do const arr = [a,b] it tries to look at what those variables will be assigned, but they’re assigned based on arr so you get a weird circular logic.

tl;dr: remove const from your second line.

Thanks ArielLeslie - yes thats point i was missing about the scopes. thanks much for your detailed explanation.!!

FYI - You can solve this challenge without declaring arr as [a, b]. You only need a single line of code for this challenge.

“When you do const arr = [a,b] it tries to look at what those variables will be assigned, but they’re assigned based on arr so you get a weird circular logic.”

thinking again the concept with a simpler code -

let a = 8;
(() => {
    "use strict";  
    const a  = a;   -----> error shows this a is undefined.

it is undefined, cos the moment i declared it as local variable with const a - JS no longer looks at global a.

plesase let me know if i am thinking ryt, Thanks !

Sure thanks. ! tried [b,a] = [a,b]

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


@ArielLeslie - When I run the tests with the code the OP originally posted I do not see any errors in the console, but when I run it on node, I see the reference error. Why would that be?

There must be something in the FCC environment that prevents it from surfacing. If you just paste the code in the console of dev tools you’ll see the same behavior as if you use Node for a repl environment.

:+1: thanksn for this