Destructuring Assignment to Assign Variables from Arrays

Destructuring Assignment to Assign Variables from Arrays
0

#1

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;
console.log(a);
(() => {
    "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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays/


#2

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.


#3

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


#4
    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.


#5

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


#6

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


#7

“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 !


#8

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


#9

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.


#10

@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?


#11

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.


#12

:+1: thanksn for this