Local Scope and Functions 163

Guys! I am so stuck…
Can anyone help me here please!

function myLocalScope() {
  'use strict';
  
  
  console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/local-scope-and-functions

Follow the instructions in the comments. This challenge is just about showing you something.

Thanks! I really can’t seem to be able to figure it out. I think I am following the instructions.

1 Like

What is not behaving the way you expect?

So I have passed the challenge, but still, don’t quite understand how.

function myLocalScope() {
  'use strict';
  var myVar = "Edy";

}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope

// Now remove the console log line to pass the test

What is the “‘use strict’;” ?

It really confused me…

Thanks again!

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums


“use strict” enforces Strict Mode. Read about it here. (Don’t worry if that doesn’t all make sense yet.)

thanks!! will read it!

Hi Edy.
Pay attention, because you do what was asked but don’t perceived the error…
It was asked to declare “myVar” inside “function myLocalScope”.
Ok you do this.

But now notice that you have a string not declared inside “myLocalScope”: ‘user strict’.
How do you think happens with a string missed? It make sense?
So there just a manner to fix it in this case:

myVar = ‘user strict’.

Now check the instructions on beginning of exercise:
“Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function.

When we check the code where it was wrote:

// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test.

In fact when you remove the console.log outside the function, as instructed by:

// Now remove the console log line to pass the test.

Do you understand it is referenced to the active code under
// myVar is not defined outside of myLocalScope

That is:
console.log(myVar);

Then it works rightly.