# Global Scope and Function

**URL:** <https://forum.freecodecamp.org/t/global-scope-and-function/160258>\
**Category:** General\
**Created:** [November 21, 2017, 7:01pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258 "2017-11-21T19:01:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LawGaming](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lawgaming/32/74296_2.png) [@LawGaming](https://forum.freecodecamp.org/u/LawGaming)\
**Post date:** [November 21, 2017, 7:01pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/1 "2017-11-21T19:01:45Z")

</div>

**Tell us what’s happening:**

**Your code so far**

```auto
// Declare your variable here

function fun1() {
  oopsGlobal = 5;
   myGlobal = 10;
  // 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;
  }
  console.log(output);
}

```

**Your browser information:**

Your Browser User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063`.

**Link to the challenge:**

> **[Learn to code with free online courses, programming projects, and interview...](https://www.freecodecamp.org/challenges/global-scope-and-functions)**
>
> Learn to code with free online courses, programming projects, and interview preparation for developer jobs.

---

<div class="post-metadata">

**Author:** ![LawGaming](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lawgaming/32/74296_2.png) [@LawGaming](https://forum.freecodecamp.org/u/LawGaming)\
**Post date:** [November 21, 2017, 7:02pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/2 "2017-11-21T19:02:16Z")

</div>

how do I declare myGlobal using var

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [November 21, 2017, 7:05pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/3 "2017-11-21T19:05:33Z")

</div>

The comment says “declare your variable here”. That’s where you should declare it.

---

<div class="post-metadata">

**Author:** ![LawGaming](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lawgaming/32/74296_2.png) [@LawGaming](https://forum.freecodecamp.org/u/LawGaming)\
**Post date:** [November 21, 2017, 7:07pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/4 "2017-11-21T19:07:15Z")

</div>

thank you @ArielLeslie

---

<div class="post-metadata">

**Author:** ![huancnttuet](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/huancnttuet/32/103042_2.png) [@huancnttuet](https://forum.freecodecamp.org/u/huancnttuet)\
**Post date:** [October 17, 2018, 4:02pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/5 "2018-10-17T16:02:02Z")

</div>

```auto
// Declare your variable here
var myGlobal = 10;
var oopsGlobal;
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;
  }
  console.log(output);
}

```

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [October 17, 2018, 4:21pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/6 "2018-10-17T16:21:28Z")

</div>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

---

<div class="post-metadata">

**Author:** ![Gabreal](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@Gabreal](https://forum.freecodecamp.org/u/Gabreal)\
**Post date:** [October 7, 2019, 1:49am UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/7 "2019-10-07T01:49:32Z")

</div>

![global](https://global.discourse-cdn.com/freecodecamp/original/3X/0/3/039a7ac268ef7b896458d1cc20a581128551e86f.jpeg)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:52pm UTC](https://forum.freecodecamp.org/t/global-scope-and-function/160258/8 "2021-01-16T17:52:38Z")

</div>

Closed on January 16, 2021, 5:52pm UTC


