Block Scope II question

I got the right answer but i still don’t understand.


I tried to change the ‘The Arctic’ string to another one and the console.log didn’t show the first string.
Can someone explain?

i would first fix the error on line 8, you just want to log the value of lightWaves variable, not sure why you made it a function call

without the correct output, things can be very confusing

Now, what is it you do not understand?

1 Like

Fixed the line eight.
I was testing the function.
What i’m not getting is, the instruction says it should log in the console the value either true or false, but nothing is happening.

I see Northern Lights logged to the console (caused by console.log on line 6) in the image you uploaded, if you fixed line 8, it should also log Moonlight, demonstrating block scope

as stated in the instructions, this is what should be logged. I don’t see where true and false are coming in

1 Like

Cleaned the code and made the exercise again.

I understand that when i use a variable inside an if block, that variable will only be accessible inside the condition made.
But then the code printed the second part of the function making it show both values when i called the function.
I don’t understand why it prints both, shouldn’t be just the true ?

because you have two console.logs? Showing you the different scopes of the variable with the same name

it shows that variable defined on line 2 maintains its value after the if clause, due to different scope

1 Like

I understand it now. Thank you!

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