Lesson contradiction

Tell us what’s happening:
Describe your issue in detail here.
In the Global Scope and Function lesson you stated:
“Variables which are declared without the let or const keywords are automatically created in the global scope”
But in this lesson you defined the variables in this way and it outputs a reference error. This directly contradicts what you taught in the previous lesson

  **Your code so far**

function myLocalScope() {
// Only change code below this line
myVar = 10;
console.log('inside myLocalScope', myVar);

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

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

Challenge: Local Scope and Functions

Link to the challenge:

@brose012 you forgot the let keyword on your variable declaration.

To declare a variable in Javascript you need to use the var, const or let keyword. What the challenge says is that when you declare a variable with the var keyword the variable has a global scope. The challenge ask you to declare a local variable so you can’t use the var keyword and you need to use one of the others.

Hope you find this helpful!

When you type myVar = 10; without var, let or const in strict mode it will toss that error. In non-strict mode doing that would absolutely make it a global variable, and not toss an error. This test is looking for a locally scoped variable called myVar, which you would need to use var, let or const. But you should avoid var because its an old way of declaring variables, let and const are much better and help you avoid potential bugs.

1 Like

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