FAQ: Scope - Block Scope

This community-built FAQ covers the “Block Scope” exercise from the lesson “Scope”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Block Scope

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why is the piece of code // console.log(lightWaves); as the last task in this thread implies, only correct if we type it in as a comment?

My understanding was that we should log the value of lightWaves to the console from outside the function as a code, not a comment but entering it as a part of the code does not generate the correct result (the ‘checkmark’ - passing the last step).

4 Likes

What I found confusing here was the switch to " define a function logVisibleLightWaves() ." when the answer actually required a Variable starting with Const before declaring the variable. This was especially confusing when the 2nd question actually asked you to declare a variable using Const.

Maybe it is because this set is related to Scope and blocks but when told to define a function I would normally assume you are looking for a function and not a variable plus a function.

1 Like

Scripts generally compile on the fly, save for the first run, the compile phase when declarations are hoisted. After that, lines are parsed and compiled during execution. Functions declared as constants are rather special. They get parsed and pre-compiled on their first call. That is, they never need to be parrsed a second time as they are held in memory in compiled form for the duration of the session.

For me, the task was never being marked as complete and was stuck with loading spinner. I tried several times and then found your post. What worked for me was entering it as a comment, get a fail, then uncomment and try again and finally it worked.

Hi, everyone. I’m learning JavaScript currently and at the moment I’m learning scopes. I have a question about this task Task.

const logSkyColor = () => {
  let color = 'blue'; 
  console.log(color); // blue 
};

logSkyColor(); // blue 
console.log(color); // ReferenceError

When we invoke function logSkyColor(), why do we get “blue” value? I thought that we could get a value only if we give arguments to our function…

Whether or not a function accepts parameters is optional. A function can do its own thing, and be done, or return a value to the line of code that called it (the caller) whether it requires parameters or not. It’s up to the programmer to decide based on what the purpose of the function is.

In the example provided, it is important to note that the function didn’t return anything even though it could have. The function itself logs “blue” to the console. The color variable only has block scope, so it doesn’t exist outside of the code body { } of the function. That’s why console.log(color); throws a Reference Error. Hope this helps!

1 Like

Thank you for your answer! I got it! :slightly_smiling_face:

1 Like