Local Scope and Functions Unexpected Behaviour

I declare myVar inside of myLocalScope function and it is still accessible from outside the function. The test checks correct. Should it?

Your code so far


function myLocalScope() {
'use strict'; // you shouldn't need to edit this line
var myVar =5;
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:

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

Challenge: Local Scope and Functions

Link to the challenge:

I think something about the curriculum update made the console a bit weird. It seems to print things more than once now when it didn’t do that before. So I think it just looks like myVar is printing the second time in the code.

But if you change that second console.log line to something different like console.log(myVar + 1) which should print 6, you will see that the console still just has 5 and 5. So it’s just the one inside the function that is printing.

1 Like

The variable myVar is not actually accessible outside the function. It’s passing the test even though you didn’t delete the console.log. The test window is not showing the error that shows up in a real console. If you open your browser’s console (F12) you will see what happens when you try to console.log(myVar) outside of the function:
image

1 Like

Thank you! It does exactly what you say. I forgot I could modify console output like that.

1 Like

It would make sense for the comment to remove the console.log. If you like, you can open a GitHub Issue for this.

Thank you for the F12! It’s a surprise for me that the real console and freecodecamp console are different.
I get somewhat different console outputs in the next task on global/function scopes:

(https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/global-vs.-local-scope-in-functions)

I guess I should run the code in my browser as well, to get a better understanding of what’s going on.

You don’t actually have to copy-paste the function into the console if you don’t want to. You can click the “Run All Tests” button in freeCodeCamp and observe your browser’s console.

1 Like

Cool, thank you!
Hitting “Run the Test” reproduced FCC console output. It seems one time isn’t just good enough not the evaluator :slight_smile: I’m not sure how it works, I just hope it will make sense sometime.

I’m not sure why it appears so much later in the curriculum, but this lesson seems relevant. The surprise factor is gone though :smile: Debugging: Understanding the Differences between the freeCodeCamp and Browser Console

1 Like

Each test in the test suite runs the function. If there are 3 tests, you’ll see the function run 3 times.

1 Like

Thank you!
So when I run the code from the browser console, it doesn’t call for evaluator and executes just once.