I thought it was a global variable WITHOUT 'var'?

Tell us what’s happening:
Okay so I don’t actually have a problem completing the task, I’m just a bit unclear about the wording of it. It says " Using var, declare a global variable named myGlobal outside of any function. ", I thought it was a global variable if you declared it without the ‘var’? Is this a typo or am I unclear about the explanation above the task?
Your code so far

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

function fun1() {
// 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;
  **Your browser information:**

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

Challenge: Global Scope and Functions

Link to the challenge:

@zemezewo I think you are unclear what it mean. There are only three keywords to declare variables:

  • var which exited in ES5 and still exit.
  • let which exited in ES6 same with var but there are a few different.
  • const which exited in ES6 mean “constant”.

And you can’t create variables without those keywords.

Here are some link for more:

oopsGlobal is declared without ‘var’ in function 1 though?

Hi @zemezewo ,

The above article explains this question in detail. Hope it helps.

I read a little about variable scopes on w3schools, and I think I’m a little clearer on the topic now. Where the variable is declared can also determine if it’s local or global and not the way it is declared i.e if declared in a function it is local to that function and is not accessible outside that function but if the variable is declared outside a function it’s a global variable which is accessible anywhere in your code. So even if

is declared without a ‘var’ it’s still going to be local because it is within a function.

myGlobal is global because it is declared in the global scope, outside of everything

the variables created without a keyword inside a function are a different thing, and that’s the case for oopsGlobal

Thank you, I saw the same thing on w3schools but this stack post provided more insight. :+1:

Yes, I understood this after checking the w3schools site, thank you for replying :+1: .

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