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

**URL:** <https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 20, 2023, 2:12pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290 "2023-05-20T14:12:52Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![TTEOTM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tteotm/32/377220_2.png) [@TTEOTM](https://forum.freecodecamp.org/u/TTEOTM)\
**Post date:** [May 20, 2023, 2:12pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/1 "2023-05-20T14:12:52Z")

</div>

**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**

```javascript
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:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![Cody\_Biggs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cody_biggs/32/358652_2.png) [@Cody\_Biggs](https://forum.freecodecamp.org/u/Cody_Biggs)\
**Post date:** [May 20, 2023, 2:19pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/2 "2023-05-20T14:19:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krze.tomas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krze.tomas/32/265321_2.png) [@krze.tomas](https://forum.freecodecamp.org/u/krze.tomas)\
**Post date:** [May 20, 2023, 2:25pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/3 "2023-05-20T14:25:02Z")

</div>

Hey there 🙂  
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](https://www.w3schools.com/js/js_scope.asp)

---

<div class="post-metadata">

**Author:** ![TTEOTM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tteotm/32/377220_2.png) [@TTEOTM](https://forum.freecodecamp.org/u/TTEOTM)\
**Post date:** [May 20, 2023, 2:26pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/4 "2023-05-20T14:26:43Z")

</div>

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

```auto
// 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 ?

---

<div class="post-metadata">

**Author:** ![krze.tomas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krze.tomas/32/265321_2.png) [@krze.tomas](https://forum.freecodecamp.org/u/krze.tomas)\
**Post date:** [May 20, 2023, 2:30pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/5 "2023-05-20T14:30:04Z")

</div>

its not really defined, console.log this

```auto
console.log(fun1());

```

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

---

<div class="post-metadata">

**Author:** ![Cody\_Biggs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cody_biggs/32/358652_2.png) [@Cody\_Biggs](https://forum.freecodecamp.org/u/Cody_Biggs)\
**Post date:** [May 20, 2023, 2:31pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/6 "2023-05-20T14:31:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TTEOTM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tteotm/32/377220_2.png) [@TTEOTM](https://forum.freecodecamp.org/u/TTEOTM)\
**Post date:** [May 20, 2023, 2:53pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/7 "2023-05-20T14:53:59Z")

</div>

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](https://global.discourse-cdn.com/freecodecamp/original/4X/d/d/d/ddd6e4a71e267ef0e5b52a1a925e0a6b9ff44bc7.png)

---

<div class="post-metadata">

**Author:** ![TTEOTM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tteotm/32/377220_2.png) [@TTEOTM](https://forum.freecodecamp.org/u/TTEOTM)\
**Post date:** [May 20, 2023, 2:56pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/8 "2023-05-20T14:56:00Z")

</div>

I checked , but then why the below code isn’t working ☹ 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’

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![krze.tomas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krze.tomas/32/265321_2.png) [@krze.tomas](https://forum.freecodecamp.org/u/krze.tomas)\
**Post date:** [May 20, 2023, 3:03pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/9 "2023-05-20T15:03:54Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![TTEOTM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tteotm/32/377220_2.png) [@TTEOTM](https://forum.freecodecamp.org/u/TTEOTM)\
**Post date:** [May 20, 2023, 3:12pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/10 "2023-05-20T15:12:24Z")

</div>

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:

```auto
myFunction();

// code here can use carName

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

```

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

---

<div class="post-metadata">

**Author:** ![Cody\_Biggs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/cody_biggs/32/358652_2.png) [@Cody\_Biggs](https://forum.freecodecamp.org/u/Cody_Biggs)\
**Post date:** [May 20, 2023, 3:19pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/11 "2023-05-20T15:19:11Z")

</div>

> [@TTEOTM](#):
>
> ```auto
> 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);
> 
> ```

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

 ![IMG_1214](https://global.discourse-cdn.com/freecodecamp/original/4X/7/b/f/7bff7d7743a84d55e9abe3482c2620e6fd64fc65.jpeg)

---

<div class="post-metadata">

**Author:** ![krze.tomas](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/krze.tomas/32/265321_2.png) [@krze.tomas](https://forum.freecodecamp.org/u/krze.tomas)\
**Post date:** [May 20, 2023, 3:22pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/12 "2023-05-20T15:22:24Z")

</div>

Thank you for your response and info, I stand corrected

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [May 20, 2023, 3:27pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/13 "2023-05-20T15:27:47Z")

</div>

The editor that freeCodeCamp uses typically forbids undeclared variables.

---

<div class="post-metadata">

**Author:** ![TTEOTM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tteotm/32/377220_2.png) [@TTEOTM](https://forum.freecodecamp.org/u/TTEOTM)\
**Post date:** [May 20, 2023, 3:32pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/14 "2023-05-20T15:32:18Z")

</div>

Thank you for verifying.

---

<div class="post-metadata">

**Author:** ![renan.young](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@renan.young](https://forum.freecodecamp.org/u/renan.young)\
**Post date:** [July 13, 2023, 4:40pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/15 "2023-07-13T16:40:13Z")

</div>

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."

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/e/f/a/efa4f14f798cf9df969e5c9333132905ed8b7e1f.png)

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

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [July 13, 2023, 4:47pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/16 "2023-07-13T16:47:19Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [July 13, 2023, 6:11pm UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/17 "2023-07-13T18:11:43Z")

</div>

> **[Strict mode - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)**
>
> JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting...

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [January 12, 2024, 6:12am UTC](https://forum.freecodecamp.org/t/basic-javascript-local-scope-and-functions-global-scope-without-let-const/611290/18 "2024-01-12T06:12:13Z")

</div>

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