Basic JavaScript - Global Scope and Functions

Tell us what’s happening:
Describe your issue in detail here.

Is there any section of the JavaScript tutorials where it teaches you that you can initialize a global variable by just saying x=y without a let, const or var?

I’ve gone though this course several times but I always seem to get tripped up here and would like to know if I’m the one missing something or if there is a better description that could be used in this module?

Your code so far

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

function fun1() {
  // Assign 5 to oopsGlobal here
/*
Which javascript lesson teaches you that you can create a variable without var,let, or const?
*/
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);
}

Your browser information:

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

Challenge: Basic JavaScript - Global Scope and Functions

Link to the challenge:

I’m confused… That’s what this Challenge is about?

Variables which are declared without the let or const keywords are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with let or const.

Agreed that’s what this module is about, but I don’t believe any of the lessons before this explicitly stated that you could initialize a variable without a variable keyword.

We have been told not to use var unless you’re supporting an older browser, use let or const, but I don’t recall ever being told explicitly that you can simply say “x=y” to initialize variable x without a keyword value preceding it.

Please keep in mind that this is from a complete coding novice perspective.

None of the previous lessons said that because this is the lesson where being told this was possible (but an awful idea) was introduced.

Okay yes I understand that, but we have been taught to declare variables a certain way in these lessons , then we are told to initiate a variable in a way we have not been taught and would not know was possible.

Instead of saying “initiate a variable without let, const or var” the lesson should say
“It’s possible (but also a poor idea) to declare a variable without let, const or var. you can do so by just saying value = x”

Otherwise a user doesn’t know what to do unless they look at the answer.

That’s what this part is talking about though?

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