Basic JavaScript - Local Scope and Functions (Global Scope without let/const)

Tell us what’s happening:
In the previous lession we have been told :

Variables which are declared without the let or const keywords are automatically created in the global scope.

If I define myVar in function myLocalScope() below without using ‘let’ or ‘const’, wouldn’t it be automatically global ?

Your code so far

function myLocalScope() {
  // Only change code below this line
      myVar=9;  //like here without using let or const keyword.

  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

why wouldn’t console.log print 9 if it is in global space ?

Challenge: Basic JavaScript - Local Scope and Functions

Link to the challenge:

1 Like

So you need to read the above part of what you have

“Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.”

If a variable is outside of a function its global scope, but not of its inside the function like you have

Hey there :slight_smile:
No matter the scope you will still have to declare it. Other than that, no it would not make it global, here are some good infos on scopes:
Javascript scopes

1 Like

In previous lesson:" Global scope and functions"
I had code:

// Declare the myGlobal variable below this line
let myGlobal=10;

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

}

// Only change code above this line

function fun2() {
  let output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

Here oopsGlobal is defined in fun1 without let/const keywords but it still can be used inside fun2 , isn’t it because we haven’t used let/const keywords ?

its not really defined, console.log this

console.log(fun1());

The reason you dont have the error from the second function because you used conditioning to output whatever is not undefined

So you are right, I wasnt looking closely enough. For some reason I didnt register what you were asking lol. Yeah, so if you look at the link posted earlier you will see the automatically global example.

fun1 doesn’t have return value so I am guessing console.fun1 probably shouldn’t be getting the correct output .
However the point i am trying to ask is that even if fun2 is checking whether oopsGlobal is not undefined it still knows the there is a variable oopsGlobal outside scope of fun1 and it’s not giving the error like “oopsGlobal is not defined”
The correct output of the previous code I gave in previous post is:
image

I checked , but then why the below code isn’t working :frowning: like why second console isn’t giving 9 and instead giving “myVar not defined” isn’t myVar global so it should be giving output as ‘outside myLocalScope 9’

function myLocalScope() {
  // Only change code below this line
      myVar=9;  //like here without using let or const keyword.

  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

Correct me if I am wrong:
I have copied your previous code where you write:

oopsGlobal=5;
  • I have renamed it to ooops
  • I have deleted that line
    and it still checks if oopsGlobal is undefined, so for me it means it does not neccesseraly mean that it knows that some undeclared name exists, but it checks for it. But as said, correct me if I am mistaken

even if it checks for all variables regardless if they are defined or not it’s still giving output in console as

oopsGlobal: 5

so it does uses the value defined in fun1 taken it as global and reuses it in fun2 to get the output.
Also in link from your first post there is a section on automatically global.

Automatically Global

If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.

This code example will declare a global variable carName, even if the value is assigned inside a function.

Code:

myFunction();

// code here can use carName

function myFunction() {
  carName = "Volvo";
}

Here carName is global variable even though it is defined in myFunction scope.

When I put this in a codepen it works fine. It shows 9 instead of undefined

1 Like

Thank you for your response and info, I stand corrected

The editor that freeCodeCamp uses typically forbids undeclared variables.

1 Like

Thank you for verifying.

According to this page you shared: " If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable."

So, this reference error should not be occurring. Should have worked, just like in codePen, as Cody_Biggs shared.

That is not always true. Undeclared variables are prohibited in “strict mode”, which freeCodeCamp uses in all of our JS curriculum because it prevents learners from writing certain types of bugs (like this very one).

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