I thought a local variable without keyword can be accessed globally?

Tell us what’s happening:

In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.

I’ve declared oopsGlobal in the function without the var keyword, but why does the console still return undefined?

Your code so far


var myGlobal = 5

function fun1() {
oopsGlobal = 10
}

console.log(myGlobal)
console.log(oopsGlobal)

And can somebody also explain why is it necessary have all those codes in fun2? Instead of just console.log to display them?

// Declare the myGlobal variable below this line
var myGlobal = 5
function fun1() {

  // Assign 5 to oopsGlobal Here
oopsGlobal = 10
}

// Only change code above this line

function fun2() {

  var output = "";

  if (typeof myGlobal != "undefined") {

    output += "myGlobal: " + myGlobal;

  }

  if (typeof oopsGlobal != "undefined") {

    output += " oopsGlobal: " + oopsGlobal;

  }

  console.log(output);

}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36 Edg/92.0.902.78

Challenge: Global Scope and Functions

Link to the challenge:

That variable would be set at runtime. You never call that function so that line doesn’t get run so it never gets added to the global scope. If you add fun1() before your console.log, it should work.

And can somebody also explain why is it necessary have all those codes in fun2 ? Instead of just console.log to display them?

Can you be more specific? Do you mean manipulating the output string?

Thank you for your reply. I’ve called the function now indeed it showed the expected output.


var myGlobal = 5

function fun1() {
oopsGlobal = 10
}

console.log(myGlobal)     //5
fun1()
console.log(oopsGlobal)   //10

Another problem is why the exact same code doesn’t display the same output in the editor pane of any other challenges. For instance, I’m copying the whole code in current challenge, go to next challenge, paste it on the editor but there the console displays nothing.

It’s probably some kind of strict mode in that challenge. Normally assigning a variable that hasn’t yet been declared is a bad idea. That is why the variable has the word “oops” in it. (In English, “oops” is an exclamation one makes after realizing that one has made a mistake.)

You should always declare your variables before using them. If you add var oopsGlobal to the top so it is declared, it should work as expected.

But as I said, that challenge was an example of a bad practice. But it is important to know what happens in that case. If you’re set strict mode, then it may work differently. Also, in “real world” coding, often linters will catch mistakes like that as you type. But at least be aware that it is a bad practice.

1 Like

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