Basic JavaScript - Global Scope and Functions

I passed this challenge, however, it mentioned the following:

"Variables which are declared without the let or const keywords are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with let or const .

It then goes on to to ask this as part of the challenge:

Inside function fun1 , assign 5 to oopsGlobal without using the var , let or const keywords.

My question is why are you saying to always declare a variable using let or const but then asking me to declare a variable without using let or const? Am I missing something or did you just want me to know that I could but I that I shouldn’t?


// Declare the myGlobal variable below this line

function fun1() {
  // Assign 5 to oopsGlobal Here


// Only change code above this line

function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Global Scope and Functions

Link to the challenge:

That, and this code shows you what happens when you create an accidental global. You get access to that variable all over the place in your code, which can lead to subtle to find bugs.

1 Like

The instructions actually said

Using let or const , declare a global variable named myGlobal outside of any function. Initialize it with a value of 10

So no, they do want you to use let or const to declare myGlobal.

myGlobal is an intentional top-level variable. It is the type of variable you create when you want to have access to anywhere in the program.

oopsGlobal is an unintentional global variable. It is automatically created by the runtime when it sees an assignment to an undeclared identifier. Its location on the global object is not expected when the identifier is inside a function or block scope. The identifier oopsGlobal is inside a function, as such, its expected scope is the function and not the global.


Yes thanks But it also went on to ask this:

Inside function fun1 , assign 5 to oopsGlobal without using the var , let or const keywords.

Yes but “assign” means to give a value. It is not the same as “declare”.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.