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).


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!


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

1 Like

Hi everyone,

I’m a little confused on the first part of this code when it’s run:

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';


When the logVisibleLightWaves() function is called, shouldn’t it just assign the variable ‘Moonlight’ to lightWaves, return lightWaves (‘Moonlight’) in the console, then stop running? Why does it return ‘Undefined’ in the console after ‘Moonlight’?

I might be understanding it… If instead of the console.log(logVisibleLightWaves()); I called the function, logVisibleLightWaves() it would display ‘Moonlight’ in the console. But because it’s within a console.log() call, it runs through the function and displays ‘Moonlight’ because of the console.log(lightWaves) within the function, but then trying to call the function within the console.log at the end can’t locate the lightWaves variable since it’s within the function.

Still a little bit to wrap my mind around, so if anyone has a better way of explaining it I’m all ears (err… eyes)!

Hello, @superschooler.

Welcome to the forum.

You are partially correct. The first part of your analysis is spot on. The reason ‘Undefined’ is also printed to the console is that your function doesn’t explicitly return anything. A function that doesn’t explicitly return a value will implicitly return ‘Undefined’.

Try altering your code a bit just for fun:

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  return 'Your function has successfully executed.'; //add this line

//Your function has successfully executed.

It is important to always remember that when a return statement is executed, control passes back to the line of code that called the function (the caller), so any code inside the function that follows a return statement will not be executed.
For example:

const logVisibleLightWaves = () => {
  const lightWaves = 'Moonlight';
  return 'Your function was prematurely terminated by this return statement.';
  console.log(lightWaves); //this line will NEVER be reached

//Output: Your function was prematurely terminated by this return statement.

That makes a lot of sense, thanks so much for the explanation! I didn’t know that a ‘return’ statement was the missing factor that caused the ‘undefined’ error.

1 Like

You’re welcome!

It’s not actually an error. It’s just what is returned when we don’t include an explicit return. Frequently we have no need to return anything from a function. As you’ve already realized, since the function itself prints your desired outcome instead of passing the function call as an argument to console.log(), you should simply call the function.

In the Block Scope exercise I wrote the code below as instructed. I understand that lightWaves = ‘Moonlight’ gets logged onto the console but I dont know why trying to log the variable lightWaves itself gives an undefined message.

function logVisibleLightWaves(){
const lightWaves = ‘Moonlight’;

It starts with {}s. Anything that is wrapped in curly braces is known as a block whether it is a function, a method, an if statement or switch, or a for or while loop. The brackets represent a namespace in which we can house locally scoped variables that cannot leak into parent scope.

const and let declare block scope for their variable(s).

let a = 42;
  let a = 6;
  // <- 6
// <- 42

The code inside the block executes immediately so the only difference is the scope.

1 Like

If you declare a variable inside of a function that’s inside of another function, will it be available in the scope of the outer function?

Short answer: No. Access can only be traced up the scope chain, not down.

1 Like

why do we need “const” when typing a new function within one line?

we don’t need it when we write
// function nameOfTheFunction()

I’m confused because I got errors everytime I don’t put “const” before my function name

If you are getting an error then it is from the lesson checker, not the interpreter.

function foo () {}

The above is a declared function. There is no other way to write it.

 foo = function () {}

That is a function expression. We would want to declare it with var, let or const, with const being the best choice since it protects the function object.

 const foo = () => {}

Also a function expression but lacking some of the features of the two above, which are essentially identical in behavior. They have a this and arguments object, and can be used as constructors. Arrow functions cannot.

1 Like

So I did a little digging partway through the scope lesson, and it turns out that variables defined using var are function scoped while the newer let and const are block scoped. It has already been stated that var is basically redundant but that we should know it because we might run into code written before ES6. Wouldn’t it be prudent then to also explain the scope differences between the different ways to define a variable? Am I getting ahead of myself or am I totally wrong about the function scope thing??
Any help would be much appreciated.