Testing: Global Scope and Functions in Repl.it

I am struggling to understand the concepts in this lesson. I understand how it all therotically works, however when I run the code in the Repl.it environment, it comes back as undefined. Shouldn’t the quiz return the value of the output rather than being undefined?

There is probably a very simple answer but I have read/ searched with no luck. Here is my code.

// Declare your variable here
var myGlobal = 10;

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

// Only change code above this line
function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


Thanks. Noted for future

The replt shows undefined, because you have not called a function. If you write:


then you will see something else.

So when I call the fun2 function, I get the result myGlobal = 10. Why is it that when I call the fun1() function, it is not defined? Thanks for your reply

The fun1 function does not log anything to the console. If you recall, any function which does not have an explicit return statement will automatically have undefined returned. That is why you see undefined on the last line of the node display.

So to confirm, the reason fun2() funtion prints the result of "myGlobal: " + myGlobal; vs " oopsGlobal: " + oopsGlobal is because the var MyGlobal value of 10 overrides the oopsGlobal value of 5. Is this correct?

I am just confused because “Variables which are used without the var keyword are automatically created in the global scope.” Under this logic, I just thought it would print the oopsGlobal value.

The reason myGlobal: 10 is displayed in the console (if only fun2 is called), is because myGlobal is defined in the global scope and initialized with the value of 10. Since fun2 can access the global scope, the if statement evaluates to true and displays the 10.

The 2nd if statement of fun2 evaluates to false, because oopsGlobal is not defined in the fun2 function or in the global scope.

However, if you call fun1 before calling fun2, then oopsGlobal gets defined in the global scope because it does not have var in front of it. In this situation, the following would be display:

myGlobal: 10 oopsGlobal: 5

Thats what I thought should happen. But when I call fun1(), it returns undefined value.

There is no console.log statement inside fun1, so you are not going to see anything displayed, but as I mentioned in a previous reply, any function which does not have an explicit return statement, will return the value undefined. For example, add the following to the end of the fun1 function and you will see something other than undefined.

function fun1() {
  // Assign 5 to oopsGlobal Here
  oopsGlobal = 5;
  return 'conicbe';

I understand! Thanks you so much for taking the time to explain. Much appreciated :slight_smile: