FAQ: Scope - Global Scope

This community-built FAQ covers the “Global 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 Global 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!

Has anyone else noticed that even though you get the code 100% correct, it still gives syntax errors and says that you fail the instructions? I’ve had this problem in literally 99% of the lessons. This is EXTREMELY DISCOURAGING, makes me think i’m just absolutely horrible at coding because even when comparing the solution code to my own, i cannot see any mistakes.

11 Likes

Agreed. Wish it would at least let you know which parts or lines you have correct so far. I have to reveal the answer too early on for something so dumb or not even incorrect, and then I am not able to follow along and complete it.

2 Likes

Thank god i’m not alone, it’s a very sad thing. Maybe if we start a few different threads in the get help part, telling people to come to that thread to do just that so they can get help from the people who have already done it and learned what was needed and can help them understand. I have a horrible problem with string methods for this exact purpose. It would be nice if we could ‘talk’ CodeCademy into doing something along those lines. Sorry you’re having problems, I decided to gain more context by just looking at a few videos and just practicing with them, picking apart the error and going back to basics. If I can help at all, let me know.

1 Like

Yeah! It just did it to me on #2 on loops as well. Urgh! I thought I saw a “report a bug” link on here somewhere but I would be more busy reporting that and not working lol
Also there was one I was trying to reset back to default to start over since it required editing the original syntax, it was one about mayo or guacamole I don’t remember. I closed and reset my browser, rebooted, still would not reset!! I had to reveal the answer to get it back. Very annoying.

1 Like

:confused: I don’t get why they don’t have a thread on here for reports like this. Set it up so if a report gets enough ‘upvotes’ (each upvote being a person who had that problem) it gets submitted to someone with impact for review. Also, the “Get Help.” button at the bottom right of the screen has a “Reset Exercise” If you click that it should completely reset, just cut or copy your code and edit it after it’s reset.
EDIT When i was having that problem, i ended up just opening a text editor and pasting it in there in case something happened, so, try that too lol

1 Like

So, I’ve tried two scenarios with the code, and both passed. What I’m curious about is if ‘const’ is technically replacing having to write ‘function’. I think I’m getting it, but am worried I’m learning the wrong way to write code properly. I need help understanding the difference, if any, of the following two ways I wrote this:

const logCitySkyline = () => {
  return 'Night Sky: ' + satellite + ', ' + stars + ', and ' + galaxy;
}

and

function logCitySkyline() {
  return 'Night Sky: ' + satellite + ', ' + stars + ', and ' + galaxy;
}

Not exactly. the => is replacing that keyword. Consider,

function foo (bar) {

}

change to expression form…

var foo = function (bar) {

};

now to arrow function…

const foo = (bar) => {

};

Note that the latter is also a function expression. Expressions are values and be used as arguments in function calls.

6 Likes

I greatly appreciate the speedy reply. Just to regurgitate: your first example is a function. The second one is a variable containing a function, as well as the third. Is that right?

I’m just getting started learning the variations, and ways to use the code. I guess my biggest issue is to know when I should use one over the other… or when one way of writing the code is just another way of doing the exact same thing.

… a function declaration.

Variables are sometimes described as containers where objects are stored, but that is not accurate. Variables are names in a table that refer to a value somewhere in memory. The table will contain a name and an address or possibly object id that can be further traced to an address where the value or object is actually stored.

That is what is meant when we say, a variable refers to an object or value. The object itself is not named in typical expressions, but is named in a declared function.

Function expressions that use the function keyword behave exactly the same as declared functions and have the same properties, such as this, arguments and return, among others. Arrow function expressions do not have all those properties (as in, do not have this or arguments objects).

It gets a little more complicated once we progress into OOP, but we can save that until later. All the technical details are of no real importance at this point. More important to learn about scope, and perhaps hoisting as it relates to declared functions and variables.

Further on in your study you will come across three terms,

non-method functions
methods
constructors

Likewise, add this to your extra reading as you progress.

When do we use one or the other form? I would suggest we should not think subjectively about this question, even though it does come down to choice. When we can reduce a function to arrow syntax without breaking the behavior expected from the function, then go ahead and use it.

const foo = bar => bar;

The above is known as an identity function that returns the argument. It is written in its simplest form.

const <- declaration keyword
foo   <- variable name
bar   <- non-parenthesized single parameter
=>    <- function keyword alias
bar   <- implicit return value
;     <- end of statement token

It will take a lot of practice and learning different usage cases to really nail down the criteria for choosing which form to use. When in doubt, just use a normal expression and when it comes time to refactor your code, decide then whether arrow syntax is appropriate (just as effective).

const foo = function (bar) {
    return bar;
};

There are no real hard and fast rules, so don’t get too bent around thinking of it like that. Think mainly about what works for you.

12 Likes

Man, you have no idea how much I appreciate your answer! It not only helped me understand in more detail what I just learned, but encouraged me to keep plowing forward, without trying to dissect each ‘code granule’ and functionality.

Again, thank you for taking the time to help a newbie! It’s very much appreciated.

4 Likes

I had same problems initially but i got to discover that it was as a result of whitespaces and indentations. I got it sorted.

here again we have a guessing game as to what the exercise wants.

At the top of main.js , write three global variables:

in what way are we meant to discern these are specifically const variables(as opposed to var or let)? there are many valid interpretations to this since the wording is vague, but apparently just one interpretation is correct.

not impressed

In this exercise, it doesn’t really matter what we use to declare the variables. Since none of them have their value changed, it could even be const

A goal down the road will be to limit global variables and use let in blocks to keep the variables from polluting the global scope or colliding with global variables. To protect globals (keep them from being changed) we would use const, and only have constants in the global scope.

Something we will learn in due course will be data structures such as arrays and objects. Often we will declare them with const to protect the structure. The elements can be mutated, added to or removed, but the structure cannot be destroyed.

1 Like

i understand the difference between those things, and i know of arrays and objects.

actually attempting to use var throws errors. this is what i mean by guessing games with intention for the exercises. it’s just not good design.

thanks though

2 Likes

I’m confused about one thing in this exercise. in instruction 2 they said to have a function named … then in the main page it says something like const … = () => implying that a variable identifier or name is also a function’s name? isn’t a function’s name different from the identifier that comes after a variable.
for example:
var y = function x ()
this is a named function
but
var y = () => how is this a named function? if anything it should be an anonymous function, no?

True. In this exercise it doesn’t matter whether one uses let, var or const to declare the variables. It works either way.

Hello!
I am having issues with the Blocks and Scope lesson.
Initially, I had issues with section 2/7 (the New York City Skyline exercise) and had to get the solution early on. Now onto 3/7, I am having the same problem. The photo shows the ‘error’ that am I receiving, yet whenever I choose ‘View Solution’, I don’t see the error in my coding for this step! and unfortunately, the ‘View Solution’ just solves the whole exercise when ideally it should show the solution for that particular step.
I would appreciate any help or insight into what I am missing or messing up! thanks

I’m having the same problem. Have you found a solution?

I think its just because in the return statement, after satellite you used a ’ . ’ when the exercise calls for a comma ’ , ’

1 Like