FAQ: Scope - Practice Good Scoping

This community-built FAQ covers the “Practice Good Scoping” 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 Practice Good Scoping

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!

A little off the scoping topic but not totally. I used a standard if statement on this lesson and got the desired results. However I was trying to give myself some practice with the ternary operator and cannot duplicate the same result. I’ve tried the following ways.
lightWaves = region === ‘The Arctic’ ? (‘Northern Lights’, console.log(lightWaves)) : (‘Moon’, console.log(lightWaves));
this gives me Moonlight, undefined as a result
I’ve also tried
region === ‘The Arctic’ ? (‘Northern Lights’, console.log(lightWaves)) : (‘Moon’, console.log(lightWaves));
With a result of Moonlight, Moonlight.
both these are still being ran inside the function.
What would the correct ternary operator expression be to achieve this same out come?

1 Like

We should not log the variable we are assigning to from inside the ternary. Let the value be assigned, then log the outcome.

lightWaves = region === 'The Arctic' ? 'Northern Lights' : 'Moon';
1 Like

So, with block scope, in the example of ‘Practice Good Scoping’ would there ever be a situation where:

let color = ‘blue’

would be considered global to the function? I see that within the if statement that color is redefined as pink. or was that a condition of creating a new variable called color within that block via the:

let color = ‘pink’

meaning, if they just said:

color = ‘pink’

would that have updated the original color = ‘blue’ to be color = ‘pink’ ?

thus, in my mind, i would think that is a global variable within that function only. thank you


Yes, when it is written in global namespace as a property of the window object. That would be its containing block, and top of scope for the whole shebang. We call that the top of the scope chain.

The scope it belongs to will be determined by the block it is in.

 > function foo() {
       let bar = 42
       for (let bar of [1,2,3,4,5]) {
<- undefined
 > let bar = 0;
 > foo()
<- undefined
 > console.log(bar)
<- undefined
1 Like

I don’t understand why the declarations of lightWaves are causing a syntax error. If I switch the let and var keywords in the lines that declare lightWaves with each other, the error disappears. The program then prints:
Northern Lights

I really don’t understand why the problem asks me to define a new variable lightWaves with the keyword let, since lightWaves was already declared earlier before the if statement. How come this doesn’t produce an error but the code below does, when I declare a lightWaves with let in the function and then another lightWaves with var?

const logVisibleLightWaves = () =>
let lightWaves = ‘Moonlight’;
var region = ‘The Arctic’;

if (region === ‘The Arctic’)
var lightWaves = ‘Northern Lights’;

I’m not sure if I am missing some instructions but exercise 6/7 instructs as so:

Inside the function body of logVisibleLightWaves() , beneath the region variable and before the provided console.log() statement, create an if statement that checks if the region is the 'The Arctic' . "

The problem is that I am not sure what ‘region’ should equal. I assumed region but I am having trouble following the instructions enough to get to step 2.
Please help
I have provided the code that I input:

function logVisibleLightWaves() {
let lightWaves =‘Moonlight’;
let region = ‘The Arctic’;
if (region === ‘The Arctic’){}}

That is correct, so far. Now just fill in the action that should take place inside the if block.

This may be a segue, or it may be what the author wants us to see, but either way…

if (region === 'The Arctic') {
    let lightWaves = "Northern Lights";

If we log or return lightWaves, what will it be?

1. Moonlight
2. Northern Lights

Give it a guess…

It would be ‘Nothern Lights’. I see it now. The problem was that the code did not auto populate for me under main.js but now it has. I changed browsers from edge to chrome, not sure if that was the fix.

Thank you for you help

Are you sure? Remember, let has block scope so the variable is inaccessible outside of the block. Mine was a trick question, but with express meaning.

const logVisibleLightWaves = () => {
  let lightWaves = 'Moonlight';
  let region = 'The Arctic';
  // Add if statement here:
  if (region === 'The Arctic') {
    let lightWaves = "Northern Lights";


If we want to return “Northern LIghts” then we would remove let from inside the if statement.

Wouldn’t it be bad form to have a global variable and local variable with the same name? I can see reusing local variable names, like ‘num’ or ‘index’. But having 2 separate variables with the same name seems like a bad idea to me.

Local variables declared with let or const (or var) will shadow a global variable of the same name.