FAQ: Scope - Global Scope


#1

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!


#2

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.


#3

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.


#4

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.


#5

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.


#6

: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


#7

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;
}

#8

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

function foo (bar) {

}

change to expression form…

var foo = function (bar) {

};

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


#9

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.


#10

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


#11

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.