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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Hey there, if I am not wrong, you learn to do randoms in the functions portion of the JS subject. And you use is later in the Rock, Paper Scissors project, and the Number Guesser game. Look those two up. I hope this helped.
The namespace is where names (variables, functions) are resolved to their location in memory. Every execution context has its own namespace. Variables and functions that exist in the global (think BASE) namespace are globally scoped.
When thinking about scope, think execution context and accessible objects. Global is the root namespace, and any variables declared in that execution context will be scoped to their parent, hence global scope.
What wraps a variable or function? That’s the question to answer in order to determine its parent scope.
That makes more sense to me, though I needed to reread a couple of times to get a grasp on the concept haha So, if global is the root namespace, and global scope is uh…the parent…then the namespace is contained within the scope?
The namespace is where names get looked up. From there we have resolved memory locations of the objects those names reference. The execution context defines the namespace. That’s where we get scope. It relates to that execution context.
Dry reading, but do continue to ask if this is still not clear. We both need to wade through this.
Hoisting is the process of parsing identifiers from source code and adding them to the namespace, along with resolution of what they point to. In the case of function declarations, their names point to where the source code resides in memory. Literal assignments stay where they are in memory. Their type is not cast until the interpreter is called to parse them. That’s when they get a wrapper object that gives them a type.
Thank you so much! I really do appreciate your encouragement, and will continue trying. (Though I know you are an expert haha)
(I think) that is clear so far… so in other words, the “names” refer to variables and functions? And here “looked up” is a synonym for “executed”—that is, called?
Is the wrapper either a global scope or global namespace? (or is that completely off the point ahaha)
Correct. And the names have associated memory addresses where the value or function can be found. We poll variables, and call functions.
JS is very persistent and wants to believe that a name can be found it it looks hard enough. This is where the scope chain comes into play. If we poll a variable or call a function, and it is not found in the present scope, JS climbs up the scope chain to the parent scope and continues to seek out the name. This continues all the way up to the global scope. If not found, then it returns, undefined.
OH I get it now! Your explanation makes a lot of sense to me. Thank you! I actually like this aspect of JavaScript–a lot of the process sounds complicated at first, but when I really get down to the coding, I feel like it’s just a matter of knowing the keywords and trying to understand why this code works. I think that might actually be the most frustrating… most of the time it feels like I’m just asking, “why does 1 + 1 = 2” ?
Okay, so I was playing with scope, and I couldn’t get my function to work no matter how simple I was making it.
I felt like I had a grasp of everything so far, but when I try to make my own thing, it doesn’t work.
Hopefully there is a simple answer to this.
When I run my function, why doesn’t it print ‘nice’ to the console.
const logFavouriteVideoGames = () => {
let best = ‘Chrono Trigger’
if (best === ‘Chrono Trigger’){
return ‘Chrono Trigger’;
console.log(‘Nice’);
}
}
console.log(logFavouriteVideoGames())
I was trying to make a conversation type function (If best === ‘Chrono Trigger’ console.log(‘nice’), I was going to add more games to the function, but I can’t get the first step to work